vue 在使用axios中报TypeError: Cannot set property 'name' of undefined at eva

16 篇文章 0 订阅

在用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);
      })

 

 

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

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值