Masade
Masade

Reputation: 716

How to pass a data to child component only once during mounting component in vuejs?

I would like to pass data to child component but do not want to bind it when the data changes in the parent

<parent :val="myval">
     <child :initialval="val"></child>
</parent>

This code binds my initial value to myval, I just want it to be passed first time and do not want initialval to be reactive to changes of my val.

Can I do it without setting a local variable in mounted() function of child component?

Upvotes: 2

Views: 1402

Answers (2)

Roy J
Roy J

Reputation: 43899

You want to initialize the component's data from a prop:

<parent :val="myval">
     <child :initialval="val"></child>
</parent>

and in the child:

data() {
    return {
        value: initialval
    };
}

then use the component's data item (value in my example here) instead of the prop for whatever you're doing in the component.

Upvotes: 3

CodeHacker
CodeHacker

Reputation: 2138

you can use the v-once attribute

<child :initialval="val" v-once></child>

Upvotes: 0

Related Questions