If you do not want to use $emit and events there is a easier way with using Vuex.
You should define your store like below guides and then use the mutations in your modal for setting new data and use store state in your another components like the index.vue that you said that you need the modal data . Here is the simplest store for just introduction to Vuex
npm install vuex --save
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
increment (state) {
state.count++
}
}
})
Now, you can access the state object as store.state, and trigger a state change with the store.commit method:
store.commit('increment')
console.log(store.state.count) // -> 1
In order to have an access to this.$store property in your Vue components, you need to provide the created store to Vue instance. Vuex has a mechanism to "inject" the store into all child components from the root component with the store option:
new Vue({
el: '#app',
store: store,
})
methods: {
increment() {
this.$store.commit('increment')
console.log(this.$store.state.count)
}
}
Note that if you have some Axios calls or async actions, you must use the actions object instead of mutations object for defining your functions .
See below codes :
Your store schema :
state : {
APIData : null
}
mutations : {
setAPIData(state, newData) {
state.APIData = newData
}
}
In your Vue instance :
methods: {
post() {
getAPI.post('api/contact/', this.form)
.then(response => (this.data = response.data))
.then(() => (this.$store.commit('setAPIData', this.data)))
.catch(error => console.log(error.response))
},
},
In your another components you can access the new data with this.APIData as a computed property :
computed : {
APIData(){
return this.$store.state.APIData
}
}