javascript - vue 初始化数据赋值报错
问题描述
vue代码<script>import axios from ’axios’;export default { data() {return { titleList: [],} }, created() {this.axios.get(’XX’).then(function(response) { console.log(response.data); this.titleList=response.data;}).catch(function (error) { console.log(error);}); }}</script>报错
TypeError: Cannot set property ’titleList’ of undefined类型错误,不能设置未定义的属性,
数据response.data是一个对象数组我已经初始化了titleList,不知为何说他未定义,求大神解答
问题解答
回答1:this 指向更改了 你可以打印出this来看一下指向谁
解决方案
1.用箭头函数吧 2.保存this (let _this = this)
回答2:.then(res => { this.titleList = res;})回答3:
this.axios.get(’XX’) .then(function (response) { response=response.body; this.titleList=response.data; }) .catch(function (error) { console.log(error);})
这样试下。如果不行,把错误贴出看下!
回答4:this指针丢失,可以使用箭头函数,也可以用一个变量保存this let _this = this
回答5:我在使用axios请求数据的时候记得是在程序入口文件main.js里面全局引入axios类库,试试引入后用Vue.prototype.$http=axios,之后就可以在全局使用了,至于楼上给出的答案指出的this指针问题,可以试试,我习惯了es6的语法,所以项目中用的一般都是箭头函数
回答6:axios.get(’***’).then((res) => { this.titleList=res.data;});
使用这种方式试试
相关文章:
1. css - chrome浏览器input记录上次cookie信息后,有个黄色背景~如何去除!2. 请教,关于python字典,合并相同值的键的实现方法3. javascript - 如何将 windows 下编辑器中的 CRLF 替换为 LF?4. android - 京东移动端网页和其app加载的url所做的呈现不应该是完全一样的吗?5. mysql5.7就没有官方性质的详细配置文件吗?求大神告知6. Windows系统能否利用Docker使用Ubuntu吗?Ubuntu能使用本机的显卡吗?7. javascript - 求解答:实例对象调用constructor,此时constructor内的this的指向?8. html5 - 在一个页面中 初始了两个swiper 不知道哪里错了 一直不对9. css3中translate(-50%,-50%)对 transform-origin的奇葩影响?10. python的MySQLdb库中的executemany方法如何改变默认加上的单引号?

网公网安备