简体   繁体   English

未捕获(在承诺中):错误:没有String的提供者

[英]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.

相关问题 未捕获(承诺):错误:没有 GooglePlus 的提供者 - Uncaught (in promise): Error: No provider for GooglePlus 未发现(承诺中):错误:没有凭据提供者? - Uncaught (in promise): Error: No provider for Credentials? 错误错误:未捕获(承诺中):错误:ActivatedRoute没有提供程序 - ERROR Error: Uncaught (in promise): Error: No provider for ActivatedRoute 错误错误:未捕获(承诺):错误:没有布尔值的提供者 - ERROR Error: Uncaught (in promise): Error: No provider for Boolean 错误:未捕获(承诺):错误:ActivatedRoute没有提供程序 - Error: Uncaught (in promise): Error: No provider for ActivatedRoute Angular 2 No Provider或DI错误(未捕获(承诺):错误:No Provider…) - Angular 2 No Provider or DI error (Uncaught (in promise): Error: No provider …) 未捕获(承诺):没有空闲的提供者 - Uncaught (in promise): No provider for Idle 如何解决“错误:未捕获(承诺):错误:没有提供”Ionic 3中的错误 - How to solve “Error: Uncaught (in promise): Error: No provider for” error in Ionic 3 错误:未捕获(承诺):错误:没有服务提供程序-Angular 4.1.2 - Error: Uncaught (in promise): Error: No provider for Service - Angular 4.1.2 core.js:4197 错误错误:未捕获(承诺)没有存储提供程序 - core.js:4197 ERROR Error: Uncaught (in promise) no provider for Storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM