Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Initialize Data Properties with Prop Values

Tags:

vue.js

vuejs2

Still a little bit young in VueJS but I'm loving every bit of it. But now, fixated somewhere.
I want to initialize some values in data() using values passed via props. This is so that I can be able to mutate them later on, since it is not recommended to mutate props inside a component. In fact the official docs recommend this property initialization using prop values as shown below:

{
props: ['initialCounter'],
data: function () {
  return { counter: this.initialCounter }
}

I have something like the one below:

<template>
<div class="well">
    <!-- Use Prop value directly on the template: works (but of no help in initializing data) -->
    Department: {{department.name}}
    <!-- Use prop value but gotten via computed property: Works inside the template but not in the initialization -->
    Department: {{fetchDepartment.name}}
    <!-- Use the array I initialized with the prop value: Does not work -->
    Department: {{this_department.name}}
</div>
</template>

<script>
    export default {
        name: 'test',
        props: ['department'],
        data() {
            return {
                this_department: this.department
                // below does not work either
                //this_department: this.fetchDepartment
            }
        },
        created() {
            // shows empty array
            console.log(this.department)
        },
        mounted() {
            // shows empty array
            console.log(this.department)
        },
        computed: {
            fetchDepartment() {
                return this.department
            }
        }
    }
</script>

As seen in the commented sections above, the initialization is not successful. Neither does the value of this.department appear either from the created() or the mounted() hooks. And note, I can see it is defined using the Chrome Vue Devtools. So my question is, how exactly should I initialize data() attributes using props values, or which is the best way of going around this issue?

like image 722
gthuo Avatar asked Aug 29 '17 16:08

gthuo


People also ask

How do you use props value in data Vue?

To specify the type of prop you want to use in Vue, you will use an object instead of an array. You'll use the name of the property as the key of each property, and the type as the value. If the type of the data passed does not match the prop type, Vue sends an alert (in development mode) in the console with a warning.

How do I change my Vue prop value?

The value of a parent property cannot be changed inside a component, and, in fact, the updated value will be lost if the parent re-renders for any reason. To update the parent property, what you should do is $emit the updated value and listen for the change in the parent.

What is the difference between data and props?

So what's the difference between props and data? Data is the private memory of each component where you can store any variables you need. Props are how you pass this data from a parent component down to a child component.


1 Answers

I know my answer comes in late but it helps me and hopefully someone else coming here. When props' data are async:

// in the parent component
<template>
    <child :foo="bar" v-if="bar" />
</template>

That way, you render the component when props are already available making it safer to follow the guide's recommended ways to initialize data value with props as so:

props: ['initialCounter'],
data: function () {
    return {
       counter: this.initialCounter
    }
}

Happy coding!

like image 89
igreka Avatar answered Sep 18 '22 21:09

igreka