簡體   English   中英

如何在不使用“ params”的情況下在Vue同級組件之間傳遞值?

[英]How to pass values between Vue sibling components, without using 'params'?

目前,我有2個組件:

  • Proyectos.vue
  • Reuniones.vue

哪里:

  • Proyectos :這是我在App開頭顯示的組件,其中顯示了項目列表。

  • Reuniones :這是顯示項目會議列表的組件。

在Proyectos組件中,我發出GET請求,該請求返回一個對象數組,即proyectos。 使用此項目列表,我可以填寫表格。

Proyecto對象結構:

    {
        objetivo:"", 
        fecha: "",
        reuniones: [
        {
            titulo: "",
            fecha: ""
        },
        {
            titulo: "",
            fecha: ""
       }]
   }

對於每個表格行,我都有一個項目javascript對象,我想在單擊按鈕時將其發送到Reuniones組件。 進入Reuniones組件后,我將執行某些操作,例如對其進行迭代。

在此處輸入圖片說明

決定不使用參數

我正在尋找一種不必通過URL發送Id並不必重新get對象的方法。 我想使用Proyectos組件中已經包含的對象。

嘗試使用事件

我嘗試使用$emit$on使用事件$on但是由於尚未加載Reuniones組件,因此無法聽到從Proyectos組件發送的事件。

Proyectos.vue:

projectSelected (project) {
     // I issue an event with the project object
     this.$root.$emit ('send', project)
     // I call the route 'reuniones' to load the Reuniones.vue component
     this.$router.push('reuniones')
}

Reuniones.vue

this.$root.$on('send', (data) => {
     console.log (data)
})

我正在尋找的行為是能夠在組件之間使用對象。

我認為您可能會考慮稱為eventBus的全局狀態,您可以在此處閱讀

它將允許您為get請求創建make there方法,並使用計算的屬性或觀察程序監視.vue實例上的更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM