wchluxi
V2EX  ›  Vue.js

vuex 的 module 局部的 mutation 如何访问全局 state?

  •  
  •   wchluxi · Jun 22, 2022 · 2063 views
    This topic created in 1426 days ago, the information mentioned may be changed or developed.
    //	局部 module
    const Post = {
    	mutations: {
        	//该函数如何访问全局 state
            fetchPosts () {
            }
        }
    }
    
    const store = createStore({
    	state: {
        	posts
        }
    })
    
    7 replies    2022-06-23 08:43:04 +08:00
    TsubasaHanekaw
        2
    TsubasaHanekaw  
       Jun 22, 2022   ❤️ 1
    建议直接学 pinjia 放弃 vuex
    wchluxi
        3
    wchluxi  
    OP
       Jun 22, 2022
    @lingxiaoli #1 如果你希望使用全局 state 和 getter ,rootState 和 rootGetters 会作为第三和第四参数传入 getter ,也会通过 context 对象的属性传入 action 。

    文档没有介绍在 mutation 里面怎么用呀
    wchluxi
        4
    wchluxi  
    OP
       Jun 22, 2022
    @TsubasaHanekaw #2 感觉不错
    thefck
        5
    thefck  
       Jun 22, 2022
    不可以
    用 action
    cszchen
        6
    cszchen  
       Jun 22, 2022 via iPhone
    mutation 不能调用异步方法,只改变状态,正确用法应该在 action 里调用异步方法,然后提交 mutation 来更新状态
    linkopeneyes
        7
    linkopeneyes  
       Jun 23, 2022
    如果就是存一些全局状态的话,可以依赖注入简单封装一个也够用
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   959 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 21:51 · PVG 05:51 · LAX 14:51 · JFK 17:51
    ♥ Do have faith in what you're doing.