angular 2 OpaqueToken

前端之家收集整理的这篇文章主要介绍了angular 2 OpaqueToken前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
需要一些提供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.

这是因为您在包含父类和子类的模块之间存在循环依赖关系.

如果您将不透明标记定义到第三个模块并将其包含在其他模块中,它将起作用.

例如一个常量模块:

export let SF = new OpaqueToken('sf');

在另外两个模块中:

import { SF } from './constants';

猜你在找的Angularjs相关文章