2017-10-16 43 views
1

存在我有這個角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財產就在那裏。

+0

'@Input()nodes:any;'? – jonrsharpe

+0

它有什麼問題? – eli

+0

請將我的評論與您實際寫的內容進行比較! – jonrsharpe

回答

0

我通常以這種方式定義@input變量。

@Input() nodes: any; 

我可以看到兩個問題

  1. 從您的組件獲得undefined或沒有得到價值的看法。因此請確保父組件正在發送適當的值[nodes]="'this is a value"'

  2. 在角度組件生命週期中,確保nodes的值準時到達。

您還可以添加ngInit生命週期掛鉤,並設置情況下,默認值沒有收到任何東西

ngOnInit() { 
    this.nodes = this.nodes || ''; 
    } 
1

您可以通過兩種方式提供輸入,

@Input() hero: Hero; 
@Input('master') masterName: string; 

在你的代碼是你提供的公共財產名稱(bindingPropertyName),但沒有可用於組件類的財產,

@Input(nodes):any; 

查看更多about @Inputhere

希望這有助於!