码迷,mamicode.com
首页 > 移动开发 > 详细

在vuejs 中使用axios不能获取属性data的解决方法

时间:2018-03-12 01:08:19      阅读:1654      评论:0      收藏:0      [点我收藏+]

标签:实例   lin   keyword   serve   自动   动作   follow   值方法   -name   

Laravel5.4 vuejs和axios使用钩子mounted不能获取属性data的解决方法

//出错问题:
then 这个里边的赋值方法this.followed = response.data.followed会出现报错,什么原因呢?原来是在 then的内部不能使用Vue的实例化的this, 因为在内部 this 没有被绑定。解决:self.followed = response.data.followed;或者使用 ES6 的 箭头函数arrow function,箭头方法可以和父方法共享变量。

Here is my code:
数据属性:

 data(){
        return {
            followed : false,
        }
    },

axios请求数据:

 // mounted 方法为钩子,在Vue实例化后自动调用
    mounted() {
       axios.post(‘/api/question/follower‘, {
           ‘question‘:this.question,
           ‘user‘:this.user
       }).then(function(response){
           // console.log(response.data);
           this.followed = response.data.followed;
       })
    },

出错问题:
then 这个里边的赋值方法this.followed = response.data.followed会出现报错,什么原因呢?在Google上查了下,原来是在 then的内部不能使用Vue的实例化的this, 因为在内部 this 没有被绑定。
可以看下 Stackoverflow 的解释:

In option functions like data and created, vue binds this to the view-model instance for us, so we can use this.followed, but in the function inside thenthis is not bound.

So you need to preserve the view-model like (created means the component‘s data structure is assembled, which is enough here, mounted will delay the operation more):

解决方法:

    created() {
       var self = this;
       axios.post(‘/api/question/follower‘, {
           ‘question‘:this.question,
           ‘user‘:this.user
       }).then(function(response){
           // console.log(response.data);
           self.followed = response.data.followed;
       })
    },

或者我们可以使用 ES6 的 箭头函数arrow function,箭头方法可以和父方法共享变量 :

 created() {
       axios.post(‘/api/question/follower‘, {
           ‘question‘:this.question,
           ‘user‘:this.user
       }).then((response) => {
           this.followed = response.data.followed;
       })
    },

完整代码:

 
<script>
export default {
    // 为父组件传递到子组件的属性值,子组件使用props方法接收
    props:[‘question‘, ‘user‘],
    // mounted 方法为钩子,在Vue实例化后自动调用
    mounted() {
       /**  这种旧的写法会在Laravel5.4中报错
        this.$http.post(‘/api/question/follower‘, {‘question‘:this.question, ‘user‘:this.user}).then(response => {
            console.log(response.data);
        })
        */
       axios.post(‘/api/question/follower‘, {
           ‘question‘:this.question,
           ‘user‘:this.user
       }).then(function(response){
           // console.log(response.data);
           this.followed = response.data.followed;
       })
    },
    data(){
        return {
            followed : false,
        }
    },
    computed:{
        text(){
           return this.followed ? ‘已关注‘ : ‘关注该问题‘;
        }
    },
    methods:{
        // 关注动作
        follow(){
            axios.post(‘/api/question/follow‘, {
                ‘question‘:this.question,
                ‘user‘:this.user
            }).then(function(response){
                this.followed = response.data.followed;
            })
        }
    }
}
</script>

在vuejs 中使用axios不能获取属性data的解决方法

标签:实例   lin   keyword   serve   自动   动作   follow   值方法   -name   

原文地址:https://www.cnblogs.com/blogNYGJ/p/8547086.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!