首页 > 解决方案 > TypeError:无法读取未定义 vueJS 的属性“_wrapper”

问题描述

我的 VueJS 应用程序有点麻烦。我想要的是在加载时向服务器发送请求,响应我用户的“用户名”并在我的网站上更新它。一切正常,但是当我尝试分配返回值时,我的程序停止加载图像并且标题中的错误出现在控制台中。来自服务器的值是正确的 - 由控制台检查。这是我显示用户名的代码:

<span class="iconsNav" style="margin-left: 4px" >{{this.username}}</span>

这是我的脚本:

<script>
//components
export default {
  methods : {
    login(){
      //function to login
    },
    async getUserData(){
      var token = 'Token '+ VueCookie.get('access_token')
      const response = await axios.get('http://localhost:8000/api/account/data',{
        headers:{
          'Authorization': token
        }
        })

        console.log(response.data.username)
        //when i try execute this line error occurs
        this.username = response.data.username
    }
  },
  data(){
    return {
      username : "User"
    }
},
mounted: function(){
  this.getUserData()
}
}
</script>

标签: javascriptvue.js

解决方案


<span class="iconsNav" style="margin-left: 4px" >{{username}}</span>

要在 vue 中获取数据,您不需要this.


推荐阅读