我是angular2的新手,我遇到了这个错误.
我有这样的课
我有这样的课
@Injectable() export class VehicleDetails { constructor(private policynumber: string,private vinnumber: string) { this.policyNumber = policynumber,this.vinNumber = vinnumber } policyNumber: string; vinNumber: string; }
和另一个这样的课
export class Policy { constructor() { } emailAddress: string; vehicleDetails: VehicleDetails[] }
我已经在这样的模块中导入了我的课程
import { Policy } from './models/Policy'; import { VehicleDetails } from './models/VehicleDetails'; @NgModule({ declarations: [ LienholderComponent,AppComponent,PolicyVehicleComponent ],imports: [ RouterModule.forRoot( APP_ROUTES,{ enableTracing: true } // <-- debugging purposes only ),BrowserModule,FileUploadModule,ReactiveFormsModule,HttpModule ],providers: [Policy,VehicleDetails],bootstrap: [AppComponent] }) export class AppModule { }
当我在VehicleDetails类中注释我的构造函数时一切正常,但是当我使用构造函数时,我开始得到这个错误.
如何解决这个错误,我在构造函数中有两个字符串参数,因为我想动态添加值.
我的错误就是这个
What i want to achieve constructing the VehicleDetails class like this
is
this.policy.emailAddress='email@test.com'; this.policy.vehicleDetails.push(new VehicleDetails('valueA1','valueA2')); this.policy.vehicleDetails.push(new VehicleDetails('valueB1','valueB2')); this.policy.vehicleDetails.push(new VehicleDetails('valueC1','valueC2'));
DI无法创建VehicleDetails的实例,因为它没有任何关于私有policynumber传递的信息:string,private vinnumber:string
原文链接:https://www.f2er.com/angularjs/240503.html你可以做的是使用@Inject()装饰器
constructor(@Inject('policy') private policynumber: string,@Inject('vin') private vinnumber: string) {
并提供像这样的值
providers: [Policy,VehicleDetails,{provide: 'policy',useValue: 'abc'},{provide: 'vin',useValue: 'def'}],