一类的Angular 6依赖注入

时间:2018-06-25 17:18:23

标签: angular dependency-injection models

我有一个关于Angular 6依赖注入的知识问题。

场景如下:

单个类(用作模型):

import { NgModule } from '@angular/core';
import { ContactProviderService } from './../services/contact-provider.service';
import { SuiviInterface } from './../interfaces/suiviinterface';
import { WebapiService } from './../services/webapi.service';
import { QuotationInterface } from './../interfaces/quotationinterface';

import { DeserializableInterface } from './../interfaces/deserializableinterface';

import { MomentDate } from './momentdate';
import { User } from './user';
import { Feature } from './feature';
import { Suivi } from './suivi';

@NgModule({
  providers: [
    WebapiService,
    ContactProviderService
  ]
})

export class Quotation implements QuotationInterface, DeserializableInterface<Quotation> {
  public id: number;

  public date: MomentDate;

  public user: User;

  public feature: Feature;

  public dateValidation?: MomentDate;

 public dateProposition?: MomentDate;

 public dateAcceptation?: MomentDate;

 public isArchive: Boolean;

 public isDeleted: Boolean;

 public numParcelle: string;

 public commentaire: string;

 public lead?: string;

 public suivi: Array<Suivi>;

 public propositionDone: boolean = false;

 private nbAttributions: number = 0;
 private nbPropositions: number = 0;

 public constructor(
    private webApi: WebapiService,
    private contactProvider: ContactProviderService) {
    this.dateValidation = null;
  }

 ... some getters / setters

public deserialize(input: any): Quotation {

    Object.assign(this, input);

    // some other stuff

   return this;
 }
 }
 }

在此类中,WebapiService和ContactProviderService都是Injectable的,因此,我想将它们注入到类的容器中就足够了。

我添加了@NgModule进行尝试...但是当我创建报价实例时,出现以下错误:

Expected 2 arguments but got 0

我做错了什么?

Thx,

让·卢克

1 个答案:

答案 0 :(得分:0)

我认为这是因为您缺少@Component Decorator,然后在其中可以提供服务(如果它们是该组件所独有的)。 (如果在组件中声明,则不会与整个应用共享)

<img [src]="myobject?.URL | secure" />