簡體   English   中英

vue.js:在實施“簡單的 State 從頭開始管理”方法時,如何傳遞/訪問商店?

[英]vue.js: When implementing the "Simple State Management from Scratch" approach, how to pass on/access the store?

這可能是一個非常愚蠢的問題,但是在閱讀了 vue.js 的state 管理文檔之后,我想玩弄存儲模式。

我注意到store.state在示例中的兩個應用程序之間共享。 但是我現在如何從組件中調用 store 的setMessageAction方法呢? 商店不應該以某種方式注入/注冊到 vue 實例中,以便可以通過this從組件或類似的東西中訪問嗎?

是的,你是對的。

您應該在組件聲明中聲明您的商店,如此所述

document.js


var store = {
    debug: true,
    state: {
          message: 'Hello!'
         },
    setMessageAction (newValue) {
        if (this.debug) console.log('setMessageAction triggered with', newValue)
        this.state.message = newValue
    },

    clearMessageAction () {
        if (this.debug) console.log('clearMessageAction triggered')
        this.state.message = ''
    }
 }



var vmA = new Vue({
   data: {
    privateState: {},

    <!-- HERE YOU ARE PASSING THE STATE -->
    sharedState: store.state
    }
  })

暫無
暫無

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

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