Tôi có 3 thành phần (get-users, get-projects, get-tasks) - mỗi nút chứa một yêu cầu ajax để truy xuất một số dữ liệu. Tôi muốn dữ liệu được trả về từ yêu cầu ajax được hiển thị trong thành phần độc lập thứ tư trên trang (kết quả hiển thị). ví dụ.Làm cách nào để chia sẻ dữ liệu giữa các thành phần không phải là cha-con trong Vue
<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>
Thành phần get-người sử dụng:
<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>
Các Vue dụ/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 },
})
Là thành phần show-kết quả không phải là một phần của một mối quan hệ tôi cha/con không thể sử dụng phương thức $ broadcast hoặc $ dispatch của API.
Có thể chuyển dữ liệu từ thành phần này sang thành phần khác khi hoàn thành lời hứa ajax không?
Họ cả hai trong cùng một trường hợp Vue gốc? – nils
Có, họ đang có. Tôi đã chỉnh sửa bài đăng gốc để hiển thị khai báo Vue. –