我對 vue.js 的了解有限,但據我所知,這應該可行,由于某種原因,當我嘗試訪問資料屬性中的變數時,它找不到它。

data: function() {
return {
id: 0,
clients: []
}
},
methods: {
getClientData(){
fetch('/view-clients/' this.id).then(function (response) {
return response.text();
}).then(function (data) {
this.clients = JSON.parse(data);
this.id = this.clients[clients.length - 1].id;
}).catch(function (error) {
console.log('Error: ' error);
});
}
}
uj5u.com熱心網友回復:
函式范圍很可能是罪魁禍首。改用箭頭函式,因此this指的是 Vue 組件。
data() {
return {
id: 0,
clients: []
}
},
methods: {
getClientData(){
fetch('/view-clients/' this.id).then((response) => response.text())
.then((data) => {
this.clients = JSON.parse(data);
this.id = this.clients[this.clients.length - 1].id;
}).catch((error) => {
console.log('Error: ' error);
});
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/406972.html
標籤:
上一篇:反應本機的意外保留字“等待”
下一篇:添加第二個元素后第一個元素不切換
