0๐
If You want to:
- Share data from parent to child use props or maybe emitting custom events.
- Create an app-level shared state use Pinia or Vuex
- Share data between components use Event Bus
- [Vuejs]-Using typescript with vue 3 without using the vue cli
- [Vuejs]-Got errs with puppeteer and node-sass running npm i
0๐
As both the component are in a same page, I am assuming both are having sibling
relationship. If Yes, You can give a try to below approach :
- Pass the variable from
Component 2
to the parent via emitting an event. - Now, after capturing the variable, You can pass that variable as a
prop
inComponent 1
.
Live Demo (Just for understanding, I am using Vue 2, You can change it as per your Vue version) :
Vue.component('component1', {
props: ['variable', 'childmsg'],
template: '<p>{{ childmsg }}</p>',
mounted() {
setTimeout(() => {
console.log(this.variable)
})
}
});
Vue.component('component2', {
data(){
return{
variable2: '123'
}
},
mounted() {
this.$emit('component-2-variable', this.variable2)
},
props: ['childmsg'],
template: '<p>{{ childmsg }}</p>'
});
var app = new Vue({
el: '#app',
data: {
component2Var: null
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<component1 :variable="component2Var" childmsg="This is Component 1"></component1>
<component2 @component-2-variable="component2Var = $event" childmsg="This is Component 2"></component2>
</div>
- [Vuejs]-Vue is changing the specificity of the "*" selector by adding the data attributes, how do I work around this?
- [Vuejs]-IFrame execution faster on localhost vs IP/Domain
Source:stackexchange.com