0

I want to assign props value in data and use this data value inside mounted. right now my approach is which is not working:

dashboard.vue

<template>
    <div>
        <navigation-section :userid='userID'
                            :username='username'
                            :profilepic='profile_pic'
                            :unreads='unreads'>
        </navigation-section>
    </div>
</template>

<script>
    import NavigationSection from '../components/NavigationSection';

    export default {
        components: {   
            NavigationSection,
        },

        data() {
            return {
                posts: [],
                userID: '',
                username: '',
                unreads: [],
                profile_pic: '',
            }
        },

        created() {
            this.getNavInfo();
        },

        methods: {
            getNavInfo() {
                axios.get('get_nav_info').then(response => {
                    this.userID = response.data.userID;
                    this.username = response.data.username;
                    this.profile_pic = response.data.profile_pic;
                    this.unreads = response.data.unreads;
                })
            }
        }
    }
</script>

NotificationSection (child of dashboard.vue)

<template>
    <div>
        <notification :userid='userid' :unreads='unreads'></notification>
        <h1>{{ username }}</h1>     
        <img :src='profilepic' :alt='profilepic'> 


    </div>
</template>

<script>
    import Notification from '../components/Notification';

    export default {
        props:['userid', 'username', 'unreads', 'profilepic'],
        components: {
            Notification
        }
    }
</script>

Notification (child of NotificationSection.vue)

<template>
    <div>
        // values shows in template
        unreads array length: <div class="count" v-if='unreads.length > 0'>{{ unreads.length }}</div>
        userid : <p>{{ userid }}</p>
    </div>
</template>

<script>
    export default {
        props:['unreads', 'userid'],

        computed: {
            userID() {
                return this.userid 
            }
        },

        mounted() {
            console.log(this.userID); // ** problem : in console.log shows a blank instead of id which is working on template. 
        }   
    }

</script>

enter image description here

i have edited the question. the values are showing in template but when i want to use the props value inside mounted or event try to access data value inside script it doesnt work. advance thanks for help.

12
  • The problem doesn't appear to be in the posted code.
    – Bert
    Commented Sep 23, 2017 at 15:40
  • u mean the code is ok ?? @Bert Commented Sep 23, 2017 at 15:41
  • Yes. Here is an example. The only thing added is the template. It works. codepen.io/Kradek/pen/jGVPvK?editors=1010
    – Bert
    Commented Sep 23, 2017 at 15:42
  • i dont know .. it should work. but i cant use the data value inside mounted, created or any method. but they are working in template.. Commented Sep 23, 2017 at 15:50
  • How are you passing it? What is the template? Need more code.
    – Bert
    Commented Sep 23, 2017 at 15:51

1 Answer 1

2

You can use computed properties:

export default {
    props: ['userid'],

    computed: {
        userID() {
            return this.userid
        }
    },

    mounted() {
        console.log(this.userID);
    }
}

Or simply:

export default {
    props: ['userid'],

    mounted() {
        console.log(this.userid);
    }
}
3
  • when i am trying to access in mounted or created it the id become null or blank. this is really weird. because it shows in template :( Commented Sep 23, 2017 at 16:22
  • hmm could you update the question with your parent component template?
    – Ikbel
    Commented Sep 23, 2017 at 17:01
  • i have edited the question with parent code. kindly have a look. @Ikbel Commented Sep 23, 2017 at 17:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.