属性不会对组件
问题描述:
存在我有这个角4部分:属性不会对组件
export class MenuComponent {
constructor(private menuService: MenuService) { }
@Input(nodes):any;
getMenu(path:string): void {
this.menuService.getData(path).subscribe(data => {
// Read the result field from the JSON response.
let newValue = JSON.stringify(data).replace('{"Node":', '[');
newValue = newValue.substring(0,newValue.length - 1);
newValue+="]";
const menu=JSON.parse(newValue);
this.nodes = menu;
});
}
}
我不断收到此错误:Property 'nodes' does not exist on type 'MenuComponent'
,我不知道为什么,因为nodes
财产就在那里。
答
我通常以这种方式定义@input变量。
@Input() nodes: any;
我可以看到两个问题
从您的组件获得
undefined
或没有得到价值的看法。因此请确保父组件正在发送适当的值[nodes]="'this is a value"'
在角度组件生命周期中,确保
nodes
的值准时到达。
您还可以添加ngInit
生命周期挂钩,并设置情况下,默认值没有收到任何东西
ngOnInit() {
this.nodes = this.nodes || '';
}
答
您可以通过两种方式提供输入,
@Input() hero: Hero;
@Input('master') masterName: string;
在你的代码是你提供的公共财产名称(bindingPropertyName
),但没有可用于组件类的财产,
@Input(nodes):any;
查看更多about @Input
here。
希望这有助于!
'@Input()nodes:any;'? – jonrsharpe
它有什么问题? – eli
请将我的评论与您实际写的内容进行比较! – jonrsharpe