如何從ViewModel向View添加組件。例如,我有以下內容:Aurelia在ViewModel上設置元素
我有一個服務注入組件。然後從組件I獲取數據,當我得到響應時,我將data
屬性設置爲獲取的數據。在此之後,我想將組件加載到視圖中,並將獲取的數據視爲可綁定的。
this.service.get()
.then(response => response.json())
.then(data =>{
this.data = data.results
//Mount element to View here: <component data.bind="data"></component>
})
林問這個問題,因爲如果我設定的值時獲取並查看本:
<component data.bind="data"></component>
它通過一個空的對象。我曾嘗試使用canActivate
和activate
方法上的服務。
組件:
export class Component{
@bindable data
bind(){
console.log(this.data)
}
}
'component'看起來像什麼? –
我更新了我的問題。如果我記錄數據,它是一個空數組。 –
據我所知,這不應該是一個問題。也許有些東西會破壞你的代碼。顯示您的Web組件和服務的完整代碼 –