[英]Uncaught (in promise): Error: No provider for String
I am new to angular2 and i am stuck with this error. 我是angular2的新手,我遇到了这个错误。 I have a class like this
我有这样的课
@Injectable()
export class VehicleDetails {
constructor(private policynumber: string, private vinnumber: string) {
this.policyNumber = policynumber,
this.vinNumber = vinnumber
}
policyNumber: string;
vinNumber: string;
}
and another class like this 和另一个这样的课
export class Policy {
constructor() {
}
emailAddress: string;
vehicleDetails: VehicleDetails[]
}
i have imported my class in the modules like this 我已经在这样的模块中导入了我的课程
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 { }
when i comment my constructor in the VehicleDetails class everything works fine, but when i use the contructor, i start to get this error. 当我在VehicleDetails类中注释我的构造函数时一切正常,但是当我使用构造函数时,我开始得到这个错误。 how to resolve this error, i have two string parameter in the constructor because i would like to add the values dynamically .
如何解决这个错误,我在构造函数中有两个字符串参数,因为我想动态添加值。
my error is this 我的错误就是这个
What i want to achieve constructing the VehicleDetails class like this is
我想要实现像这样构建VehicleDetails类
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'));
so, i am getting the value dynamically and i want to bind the values to the model as explained in the above code 所以,我动态获取值,我想将值绑定到模型,如上面的代码所述
DI can't create an instance of VehicleDetails
because it doesn't have any information about what to pass for private policynumber: string, private vinnumber: string
DI无法创建
VehicleDetails
的实例,因为它没有任何关于private policynumber: string, private vinnumber: string
传递的信息private policynumber: string, private vinnumber: string
What you can do is to use the @Inject()
decorator 你可以做的是使用
@Inject()
装饰器
constructor(@Inject('policy') private policynumber: string, @Inject('vin') private vinnumber: string) {
and provide the values like 并提供像这样的值
providers: [Policy, VehicleDetails, {provide: 'policy', useValue: 'abc'}, {provide: 'vin', useValue: 'def'}],
You dont need the @Injectable
decorator according to your question update. 根据您的问题更新,您不需要
@Injectable
装饰器。 Change your VehicleDetails to this: 将您的VehicleDetails更改为:
export class VehicleDetails {
constructor(policynumber: string, vinnumber: string) {
this.policyNumber = policynumber,
this.vinNumber = vinnumber
}
policyNumber: string;
vinNumber: string;
}
and remove VehicleDetails
from your AppModule
providers. 并从
AppModule
提供程序中删除VehicleDetails
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.