错误:Uncaught (in promise) TypeError: Cannot set properties of undefined (setting 'tableData')  

源代码:

 created:function(){
    axios.get("http://localhost:8088/user/findAll").then(function(response){
 
      this.tableData = response.data
    })
  },
      data(){
        return {
          tableData:[]
        }
      }

 原因:

原因:js作用域问题

这里为回调函数,作用域会发生变化

解决:

 created:function(){
    axios.get("http://localhost:8088/user/findAll").then((response)=>{
      this.tableData = response.data
    })
  },

原因:

箭头函数:作用域继承他的父级(created)

即:此时 =>{} 中的 this 与 created 中 this 一致

但用 function 就不一致了

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐