在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指向,奈何一時半會兒也沒搞懂,留此疑惑,日后解答

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM