在vue-cli中使用axios时报错TypeError: Cannot set property 'lists' of undefined at eval

在vue-cli中使用axios拿取数据时

export default {
  data(){
    return{
      lists:{
      }
    }
  },
  methods: {
    getList(){
      axios.get('https://cnodejs.org/api/v1/topics',{
      })
      .then(function(response){
        window.console.log(response.data.data);
        this.lists = response.data.data;
      })
      .catch(function (error) {
        window.console.log(error);
      })
    }
  },
    beforeMount() {
    this.getList();
  },

报错

TypeError: Cannot set property 'lists' of undefined at eval (list.vue?51fc:19)

 

能get到数据,但无法传给lists数组

报错信息lists未定义,查找调试许久,发现是this指向问题。

在vue-cli里.then里使用function就会乱了this指向

把.then里function改用es6的写法就能正常传参了

.then((response)=>{
        window.console.log(response.data.data);
        this.lists = response.data.data;
      })

为什么用function定义就会乱this指向,奈何一时半会儿也没搞懂,留此疑惑,日后解答

 

转载于:https://www.cnblogs.com/raonet/p/11581997.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值