我对 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);
});
}
}
Run Code Online (Sandbox Code Playgroud)
函数作用域很可能是罪魁祸首。请改用箭头函数,因此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);
});
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
18910 次 |
最近记录: |