vendredi 2 février 2018

MIXIN is not working for some variables populated using AXIOS in vue js

I have a file named initUserMixin.js.The code within it is

export default {
data(){
    return {
        user: [],
        sakil: 'sakil',
    }
},
created() {

    this.initUser()
},

methods: {

    initUser(){

        let _this = this

        axios.get('/getloggeduser').then(function(response){

            _this.user = response.data

            console.log(_this.user) // this shows the user details perfectly

        })
        .catch(function(error){

        })
    }
}

}

And I have a component name Index.vue. I have imported the mixin file in my component. The code of my Index.vue component is

<script>

import initUserMixin from './../../../mixins/initUserMixin'

export default {
    name: 'index',
    components: {

    },

    mixins: [ initUserMixin ],

    data(){
        return {
            loggedUser: []
        }
    },

    created(){
        console.log(this.user) // this is not working
        console.log(this.sakil) // this is working
    },

}
</script>

The problem happens is I can print my sakil variable in y Index.vue component but not able to display the user variable. How can I do that? Any help would be appreciated. thanks in advance. I am using vuejs2 and laravel 5.5.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire