angular 2 OpaqueToken

前端之家收集整理的这篇文章主要介绍了angular 2 OpaqueToken前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
需要一些提供OpaqueToken的帮助.使用Angular 2 beta-12.如果提供者密钥是字符串,它可以正常工作,但在使用OpaqueToken时不起作用.在Child类中,SF未定义.

家长班:

  1. export let SF = new OpaqueToken('sf');
  2.  
  3. export class A {
  4. testMsg: string = 'hello';
  5. }
  6.  
  7. @Component({
  8. template: `<child></child>`,providers: [
  9. provide(SF,{useValue: A}),provide('justString',{useValue: 'hi'})
  10. ]
  11. })
  12. export class App {}

儿童班:

  1. import {Component,Injector,Inject,OpaqueToken} from 'angular2/core'
  2. import {SF,A} from './app'
  3. console.log("******",SF); // undefined
  4. @Component({
  5. selector: 'child',template: `
  6. $$CHILD Rendered$${{a}}
  7. `
  8. })
  9. export class Child {
  10. //constructor(@Inject(SF) private a: A) {} // doesn't work
  11. constructor(@Inject('justString') private a: string) {}
  12. }

我得到的例外情况:

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.

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

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

例如一个常量模块:

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

在另外两个模块中:

  1. import { SF } from './constants';

猜你在找的Angularjs相关文章