首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类中的Angular 6依赖注入

类中的Angular 6依赖注入
EN

Stack Overflow用户
提问于 2018-06-26 01:18:23
回答 1查看 370关注 0票数 0

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

场景如下:

单个类(用作模型):

代码语言:javascript
复制
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都是可注入的,因此,我认为将它们注入到类的contuctor中是自给自足的。

我添加了@NgModule来尝试...但是当我创建一个Quotation的实例时,会出现以下错误:

代码语言:javascript
复制
Expected 2 arguments but got 0

我哪里做错了?

Thx

Jean-Luc

EN

回答 1

Stack Overflow用户

发布于 2018-06-26 02:27:57

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

代码语言:javascript
复制
@Component({
   selector: 'some-selector',
   templateUrl: ['./'],
   stylesUrl: ['./'],
   providers:[ WebapiService, ContactProviderService ]
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51028774

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档