同样的问题被多次询问,但提供的建议对我没有用。
Angular 5 错误:
无法绑定到'car',因为它不是'display'的已知属性
的AppModule
@NgModule({
declarations: [
DisplayComponent,
AppComponent,
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
title = 'app';
car:Car = new Car();
ngOnInit() {
this.car.color = "black";
}
}
app.html
hello world
<display [car]='car'></display>
display.component
@Component({
selector: 'app-display',
templateUrl: './display.component.html',
styleUrls: ['./display.component.css']
})
export class DisplayComponent implements OnInit {
@Input() car : Car;
constructor() { }
ngOnInit() {
}
}
display.html
<p>
display works!
car = {{ car.color }}
</p>
汽车对象
export class Car {
public color:String = "red";
}
Angular CLI: 1.5.0
Node: 6.11.0
OS: win32 x64
Angular: 5.0.3
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
@angular/cli: 1.5.0
@angular-devkit/build-optimizer: 0.0.33
@angular-devkit/core: 0.0.21
@angular-devkit/schematics: 0.0.37
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.8.0
@schematics/angular: 0.1.7
typescript: 2.4.2
webpack: 3.8.1
感谢您解决问题的任何帮助。