在一個項目vue.js 2:
我有一個組件居住在一個.vue文件,代表一個元素列表。另外,我有一個側欄是這個列表的總結。該側邊欄是.vue文件中的另一個組件。
所以,我怎麼能保持每個間的通信它們,例如,如果我刪除從列表中元素,反映在側邊欄宣佈一個VAR的變化是元素的總數ilustrate:
邊欄.vue如何溝通兩個單獨的.vue文件?
<template>
...
<span></span> ===> here I need total of elements listed in ListElements.vue
...
<template>
ListElements.vue
<template>
...
@click="deleteEntry"
...
<template>
<script>
methods: {
deleteEntry(entry) {
//here I need to notify to SideBar.vue in order to update the total of elements in the this.entries list.
let index = this.entries.indexOf(entry);
if (window.confirm('Are you sure you want to delete this time entry?')) {
this.entries.splice(index, 1);
}
}
</script>
能否請你添加一些代碼(如果可能簡化的),以顯示這個問題? –
這可能會有幫助:https://vuejs.org/v2/guide/components.html#Non-Parent-Child-Communication –
我已經閱讀過這個,但我不知道如何保持相同的實例總線對象跨兩個組件在兩個分離.vue文件 – mos