错误-未捕获(承诺)TypeError:无法读取未定义的属性“ data”

时间:2018-11-01 01:28:31

标签: javascript laravel vue.js vuejs2

我正在开发vue.js项目,但出现错误。

  

未捕获(承诺)TypeError:无法读取未定义的属性“ data”

这是代码。

我将表单数据传递给了User类。

Login.vue

<script>
 export default {
     data(){
         return {
             form :{
                 email:null,
                 password:null
             }
         }
    },
    methods:{
        login(){
            User.login(this.form)
        }
    }
}
</script>

这是用户类

User.js

class User {
  login(data){
      axios.post('/api/auth/login',data)
          .then(res => this.responseAfterLogin(res))
          .catch(error => console.log(error.response.data))
  }
}
export default User = new User();

但是,当我将登录方法移至Login.vue且不使用User类时,没有发生任何错误。

Login.vue

 <script>
 export default {
     data(){
         return {
             form :{
                 email:null,
                 password:null
             }
         }
    },
    methods:{
        login(){
            axios.post('/api/auth/login',this.form)
            .then(res => console.log(res.data))
            .catch(error => console.log(error.response.data))
        }
    }
}
 </script>

请解释为什么以及如何将数据传递给User类?

2 个答案:

答案 0 :(得分:0)

不确定从API返回什么数据,但是仅尝试记录res或错误,然后您才能看到数据结构。

axios.post('/api/auth/login',this.form)
.then(res => console.log(res, 'success'))
.catch(error => console.log(error, 'error'))

答案 1 :(得分:0)

class User {
    login(data) {
        axios.post('/api/auth/login', data)
            .then(function(response) {
                console.log(response.data)
            })

        .catch(function(errors) {
            console.log(errors.response.data);
        })
    }
}
export default User = new User()

login() {
         User.login(this.form)
}