需要一些提供OpaqueToken的帮助.使用Angular 2 beta-12.如果提供者密钥是字符串,它可以正常工作,但在使用OpaqueToken时不起作用.在Child类中,SF未定义.
家长班:
export let SF = new OpaqueToken('sf'); export class A { testMsg: string = 'hello'; } @Component({ template: `<child></child>`,providers: [ provide(SF,{useValue: A}),provide('justString',{useValue: 'hi'}) ] }) export class App {}
儿童班:
import {Component,Injector,Inject,OpaqueToken} from 'angular2/core' import {SF,A} from './app' console.log("******",SF); // undefined @Component({ selector: 'child',template: ` $$CHILD Rendered$${{a}} ` }) export class Child { //constructor(@Inject(SF) private a: A) {} // doesn't work constructor(@Inject('justString') private a: string) {} }
我得到的例外情况:
angular2.min.js:17EXCEPTION: Cannot resolve all parameters for ‘Child'(@Inject(undefined)). Make sure that all the parameters are decorated with Inject or have valid type annotations and that ‘Child’ is decorated with Injectable.