美文网首页SAP
使用Injection Token将字符串类型的参数注入到类的构

使用Injection Token将字符串类型的参数注入到类的构

作者: 华山令狐冲 | 来源:发表于2021-01-10 10:02 被阅读0次

一个常见的错误消息:error NG2003 - No suitable injection token for parameter

构造函数里有个参数类型为string:

  constructor(@Inject('apiUrl') private myname) {
    console.log('Jerry inject: ' + myname);
   }

这个myname通过注解@Inject修饰,id为apiUrl.

谁负责注入apiUrl呢?在app module里:

  providers: [{ provide: JerrySandBoxService },
  { provide: GreetingService, useClass: EnglishGreetingService},
  {
    provide: 'apiUrl',
    useValue: 'http://localhost:4200/heros'
  }],

运行时:

相关文章

网友评论

    本文标题:使用Injection Token将字符串类型的参数注入到类的构

    本文链接:https://www.haomeiwen.com/subject/lfnjpktx.html