为了继续我在Angular 2/4中的自定义类,我已经开始了:methods in angular typescript class are null after http get,现在我想要超越一步 .

这是我的自定义原始类:

export class Book{
    id: number,
    title: string;
    author: string;
    read: integer;
    author: string;

    constructor() {
    }

    public incRead(times: integer){
        this.read = this.read + times;
    }
}

要从组件中使用此类,我使用它:

this._httpClient.Get<Book[]>(url).subscribe().map(
    (books: Book[]) => {
        const tempbooks = new Array<Book>();
        books.forEach(book => {
            tempbooks.push(Object.assign(new Book(),book);
        }
        return tempbooks;
    }
)

HttpClient not running constructor一样,这样我就可以从组件中调用incRead方法 .


现在我想添加一个“post”方法,如下所示:

export class Book{
    id: number
    title: string;
    author: string;
    read: integer;
    author: string;

    private httpClient: HttpClient;

    constructor() {
    }

    public incRead(times: integer){
        this.read = this.read + times;
    }

    public post(): Observable(boolean)
        return this.httpClient.post<boolean>(url + Book.id.toString());
}

但我无法获得注入HttpClient服务的方法 . 我试过了

const injector = ReflectiveInjector.resoveAndCreate([HttpClient]);
this.httpClient = injector.get(HttpClient)

在构造函数中但我得到一个关于创建HttpClient实例所需的进样器get方法的Backend参数的错误 .

我一直在尝试不同的选择

也许我正在使用一种糟糕的方法在Angular中创建自定义类(基于我的OOP过去的开发),还有一些其他方法可以从我缺少的组件中分离类 .

请善待,我做完了我的作业,没有发现任何让我没有“毛茸茸”代码的正确方向的东西 .