Reputation: 2108
I have reused same component at navigation menu and at peoples page
My issue is that when SearchComponent inputs in navigation updates its value Peoples page SearchComponent inputs doesnt update it's value vice versa. How should I do/achieve this in Vue JS. Below are my codes
SearchComponent
<template>
<div class="search">
<input type="text" v-model="name">
<input type="text" v-model="email">
<input type="text" v-model="age">
</div>
</template>
<script>
export default {
name: 'SearchComponent',
data() {
return {
name: '',
email: '',
age: ''
}
}
}
</script>
Navigation
<template>
<div class="nav">
<ul>
<li>Home</li>
<li>Contact</li>
<li>About</li>
</ul>
<SearchComponent />
</div>
</template>
<script>
import SearchComponent from './SearchComponent'
export default {
name: 'NavComponent',
data() {
return {
name: '',
email: '',
age: ''
}
},
components: {SearchComponent}
}
</script>
Peoples Page
<template>
<div class="persons">
<SearchComponent />
<PersonList />
</div>
</template>
<script>
import SearchComponent from './SearchComponent'
import PersonList from './PersonList'
export default {
name: 'PersonsPage',
data() {
return {
name: '',
email: '',
age: ''
}
},
components: {SearchComponent, PersonList}
}
</script>
Upvotes: 2
Views: 518
Reputation: 9228
That’s because each time you use a Vue Component, a new instance of it is created
Since components are reusable Vue instances, they accept the same options as new Vue, such as
data
,computed
,watch
,methods
, and lifecycle hooks. The only exceptions are a few root-specific options likeel
.
So if you want if you want all Instance to be affected as single component Instead, a component’s data
should not be declared as function but as object instead i.e.
data: {
name: '',
email: '',
age: ''
}
Instead of
data: function () {
return {
name: '',
email: '',
age: ''
}
}
For which in your case you have defined it as:
data() {
return {
name: '',
email: '',
age: ''
}
}
You can also find more about Component Reusing and also Organizing Components and Since SearchComponent and PersonList component are child of Peoples Component you also need to have idea how to pass data from Parent Component i.e Peoples to Child components
Upvotes: 2
Reputation: 4223
Adding to @Eli, Try this way to share the same object reference for all the instances' data:
<template>
<div class="persons">
<SearchComponent />
<PersonList />
</div>
</template>
<script>
import SearchComponent from './SearchComponent'
import PersonList from './PersonList'
const personPageData = {
name: '',
email: '',
age: ''
};
export default {
name: 'PersonsPage',
data() {
return personPageData; //Same state object referred by all instances.
},
components: {SearchComponent, PersonList}
}
</script>
Upvotes: 2
Reputation: 2071
Those SearchComponent
even if they are the same component, they are different instances. Each one of them have their own independent data. If you want both to be in sync I'd recommend to use Vuex to have a global store that they can use as a source of truth.
Upvotes: 0