I have 3 components (get-users, get-projects, get-tasks) - each contains a button which fires an ajax request to retreive some data. I want the data returned from the ajax request to be displayed in a fourth independent component on the page (show-results). e.g.
<div class="row">
<div class="col-md-6>
<get-users></get-users>
<get-projects></get-projects>
<get-tasks></get-tasks>
</div>
<div class="col-md-6>
<show-results></show-results>
</div>
</div>
The get-users component:
<script>
export default {
template: require('./get_users.template.html'),
data: function() {
return {
userList: ''
}
},
methods: {
getUsers(e) {
e.preventDefault();
this.$http.get('api/getusers').then(function (response) {
this.userList = response.data.users; // How can I also pass this to the show-results component?
})
}
}
}
</script>
The Vue instance/decalaration
var Vue = require('vue');
Vue.use(require('vue-resource'));
import getUsers from './components/get_users.vue';
import getProjects from './components/get_projects.vue';
import getTasks from './components/get_tasks.vue';
import showResults from './components/show_results.vue';
new Vue ({
el: '#app',
components: { GetUsers, GetProjects, GetTasks, ShowResults },
})
As the show-results component isn't a part of a parent/child relationship I cant use the $broadcast or $dispatch methods of the API.
Is it possible to pass the data from one component to another at the completion of the ajax promise?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…