RC5中的外部模块:无法绑定到“X”,因为它不是已知属性

时间:2016-08-17 20:05:42

标签: angular typescript

在更新RC4 =>的过程中RC5并将多个组件拉入模块中。现在,获取运行时错误:Unhandled Promise rejection: Template parse errors: Can't bind to 'X' since it isn't a known property of 'Y'

  • 如果我将组件类添加到应用程序的declarations装饰器的@NgModule()属性中,它可以正常工作。
  • 仅当组件类位于外部模块中且该模块已添加到应用程序imports的{​​{1}}属性时才会出现此错误。

这是plunkr

外部模块代码

@NgModule()

应用模块代码

export class ExampleConfig {
  public name: string;
}

@Component({
  selector: 'example',
  template: '<strong>{{ config?.name }}</strong>'
})
export class ExampleComponent {
  @Input() config: ExampleConfig;
}

@NgModule({
  declarations: [ExampleComponent]
})
export class ExampleModule {}

2 个答案:

答案 0 :(得分:2)

material2源代码中挖掘,看起来你必须向模块装饰器的exports属性添加组件:

@NgModule({
  declarations: [ExampleComponent],
  exports: [ExampleComponent]
})
export class ExampleModule {}

答案 1 :(得分:0)

无法绑定到'X',因为它不是已知属性

如果您看到此错误,请确保未在引导程序模块文件中包含组件,请执行以下操作

如果您有3个组件

it('should do something', function(done) {
  var func_spy = sinon.spy(obj, 'method');
  func_spy.on('called', function() {
    // check something
    done();
  });
}

然后在你的主引导文件中包含以下所有内容

 1. app.component.ts
 2. greeting.service.ts
 3. hello.ts