鍍金池/ 問(wèn)答/HTML/ vue 加載數(shù)據(jù)問(wèn)題

vue 加載數(shù)據(jù)問(wèn)題

vue加載數(shù)據(jù)時(shí)顯示 變量未定義

created() {
    this.getData(); 
  },
  methods: {
    getData() {
      this.$ajax.get('/api/commodityCategory/list')
        .then(function(response) {
          let list = response.data;
          this.tag1 = list;
          console.log(response.data.data);
        })
        .catch(function(response) {
          console.log(response);
        });
    },

報(bào)錯(cuò)2navSet.vue?6fae:145 TypeError: Cannot set property 'tag1' of undefined

這個(gè)tag1在data里有定義

回答
編輯回答
氕氘氚

this指向
忘記看你的ajax了/

2017年7月29日 10:52
編輯回答
遲月

this指向問(wèn)題

 getData() {
var that = this;
      this.$ajax.get('/api/commodityCategory/list')
        .then(function(response) {
          let list = response.data;
          that.tag1 = list;
          console.log(response.data.data);
        })
        .catch(function(response) {
          console.log(response);
        });
2017年2月6日 11:54
編輯回答
風(fēng)畔

這個(gè)很明顯是this的指向問(wèn)題??!你在ajax里面寫(xiě)的這個(gè)this指向的是ajax而不是vue你改成這樣試試

 getData() {
      this.$ajax.get('/api/commodityCategory/list')
        .then(response=>{
          let list = response.data;
          this.tag1 = list;
          console.log(response.data.data);
        })
        .catch(function(response) {
          console.log(response);
        });
    },
2017年12月8日 14:00