简体   繁体   English

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

[英]External Module in RC5: Can't bind to 'X' since it isn't a known property

In the process of updating RC4 => RC5 and have pulled several components into modules. 在更新RC4 => RC5的过程中,已将几个组件拉入模块。 Now, get a runtime error: Unhandled Promise rejection: Template parse errors: Can't bind to 'X' since it isn't a known property of 'Y' . 现在,得到一个运行时错误: Unhandled Promise rejection: Template parse errors: Can't bind to 'X' since it isn't a known property of 'Y'

  • If I add the component class to the declarations property of the app's @NgModule() decorator, it works. 如果我将组件类添加到应用程序的@NgModule()装饰器的declarations属性中,则它将起作用。
  • Only when the component class is in an external module and that module is added to the imports property of the app's @NgModule() do I get this error. 仅当组件类在外部模块中并且将该模块添加到应用程序的@NgModule()imports属性中时, @NgModule()此错误。

Here's a plunkr . 这是一个笨拙的人

External Module Code 外部模块代码

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 {}

App Module Code 应用模块代码

@Component({
  selector: 'my-app',
  template: '<h2>Hello <example [config]="exampleConfig"></example></h2>'
})
export class App {
  constructor() {
    this.exampleConfig = new ExampleConfig();
    this.exampleConfig.name = 'World';
  }
  protected exampleConfig: ExampleConfig;
}

@NgModule({
  imports: [BrowserModule, ExampleModule],
  declarations: [App],
  bootstrap: [App]
})
export class AppModule {}

Digging around in the material2 source, it looks like you have to add components to the exports property of the module decorator: material2源代码中进行挖掘,似乎您必须将组件添加到模块装饰器的exports属性中:

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

Can't bind to 'X' since it isn't a known property 无法绑定到“ X”,因为它不是已知属性

If you see this error, then be sure you have not included your Component in your bootstrap module file, Do the following 如果看到此错误,请确保您的引导模块文件中未包含组件,请执行以下操作

if you have 3 Components 如果您有3个组件

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

then in you main bootstrapping file include everything as follows 然后在您的主引导文件中包含以下所有内容

import { BrowserModule }  from '@angular/platform-browser';  
import { NgModule } '@angular/core';
import { MyApp } from './app.component'
import {Welcome} from './services/welcome';
import {Hello} from './hello.component';

@NgModule({
  imports: [BrowserModule],
  providers:[Welcome],
  declarations: [MyApp
                  ,Hello
                ],
  bootstrap: [MyApp]
})
export class MyAppModule {

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular2 RC5:无法绑定到“属性 X”,因为它不是“子组件”的已知属性 - Angular2 RC5: Can't bind to 'Property X' since it isn't a known property of 'Child Component' Angular 2 RC5:无法绑定到&#39;ngFor&#39;,因为它不是已知的属性 - Angular 2 RC5: Can't bind to 'ngFor' since it isn't a known property of Angular 2 RC 5 - 无法绑定到&#39;...&#39;,因为它不是&#39;...&#39;的已知属性 - Angular 2 RC 5 - Can't bind to '…' since it isn't a known property of '…' 无法绑定到“ x”,因为它不是“ x”的已知属性 - Can't bind to 'x' since it isn't a known property of 'x' 无法绑定到“x”,因为它不是“y”的已知属性 - Can't bind to 'x' since it isn't a known property of 'y' RC4 - 无法绑定到&#39;routerLink&#39;,因为它不是已知的本机属性 - RC4 - Can't bind to 'routerLink' since it isn't a known native property 在更新Angular2 rc3之后,无法绑定到&#39;ngFormModel&#39;,因为它不是已知的本机属性 - Can't bind to 'ngFormModel' since it isn't a known native property, after update Angular2 rc3 使用ng2-bootstrap与Angular 2 RC 6 - 无法绑定到无法绑定到[...],因为它不是[...]的已知属性 - Using ng2-bootstrap with Angular 2 RC 6 - can't bind to can't bind to […] since it isn't a known property of […] 角度-无法绑定到“属性”,因为它不是“ a”的已知属性 - Angular - Can't bind to 'property' since it isn't a known property of 'a' 无法绑定到属性,因为它不是元素的已知属性 - Can't bind to property since it isn't a known property of element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM