在用axios中不能给初始的name赋值,提示“TypeError: Cannot set property 'namep' of undefined
    at eva”,纠结了老半天

网上给的答案各种尝试,终于找到了答案

 

data(){
      return {
          namep:"",
          age:"14",
          sex:'男'
      }
  },
  methods: {
    onClickLeft() {
      console.log("a");
    },
    onClickRight() {
      console.log("b");
    },
    userInfo(){
   
        axios.get('/api/health/api/v2/checkReport/getPatientMe',{headers:{'token':'528720b202e54705860e28fcc468a231'}})
            .then(function (response) {
                console.log(response);
                console.log(response.data.body.name);
                this.namep=response.data.body.name;//这里始终不能赋值
                console.log(this.namep);
            })
            .catch(function (response) {
                console.log(response);
            });

    }
  },

解决方法,这个换成es6的箭头函数就能正常赋值,

axios.get('/api/health/api/v2/checkReport/getPatientMe',{headers:{'token':'528720b202e54705860e28fcc468a231'}}).then((response) => {
        this.namep=response.data.body.name;
                console.log(this.namep);
      })

 

 

每天积累一点,进步一点!

Logo

前往低代码交流专区

更多推荐