简体   繁体   English

Vue.js 错误:TypeError:无法读取未定义的属性

[英]Vue.js error: TypeError: Cannot read properties of undefined

My knowledge of vue.js is limited but as far as i'm aware this should work, for some reason when I try to access my variable in the data property it can't find it.我对 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);
        });
    }
}

Function scope is most likely the culprit. Function scope 很可能是罪魁祸首。 Use arrow functions instead so this refers to the Vue component.请改用箭头函数,因此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);
          });
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Vue.js:“TypeError:无法读取未定义的属性(读取'$refs)'” - Vue.js: "TypeError: Cannot read properties of undefined (reading '$refs)'" Vue.JS - Api 请求 - 未捕获(承诺中)TypeError:无法读取未定义的属性(读取“报价”) - Vue.JS - Api request - Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'quote') Vue.js 计算属性:[Vue 警告]:渲染错误:“TypeError:无法读取未定义的属性‘userId’” - Vue.js computed property : [Vue warn]: Error in render: "TypeError: Cannot read property 'userId' of undefined" TypeError:无法读取未定义的“已创建”属性,未定义错误“Vue” Vue.js 3 - TypeError: Cannot read property 'created' of undefined, error 'Vue' is not defined Vue.js 3 “类型错误:无法读取未定义的属性‘服务’”Vue.js + Vuefire - "TypeError: Cannot read property 'servicios' of undefined" Vue.js + Vuefire TypeError:无法读取未定义的属性“标题”。 Vue.js - TypeError: Cannot read property 'title' of undefined. Vue.js Vue.js TypeError:消息无法读取未定义的属性“ singlePost” - Vue.js TypeError: Message Cannot read property 'singlePost' of undefined TypeError:无法读取未定义的Flatpickr&Vue.js属性“ add” - TypeError: Cannot read property 'add' of undefined" Flatpickr & Vue.js Vue.js - 类型错误:无法读取未定义的属性“标题” - Vue.js - TypeError: Cannot read property 'title' of undefined “无法读取未定义事件的属性”Vue.JS - 'Cannot read properties of undefined events' Vue.JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM