wchluxi
V2EX  ›  Vue.js

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

  •  
  •   wchluxi · Jun 22, 2022 · 2510 views
    This topic created in 1595 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
    TsubasaHanekaw
    Jun 22, 2022❤️1
    2
    建议直接学 pinjia 放弃 vuex
    wchluxi
    wchluxi
    OP
    Jun 22, 2022
    3
    @lingxiaoli #1 如果你希望使用全局 state 和 getter ,rootState 和 rootGetters 会作为第三和第四参数传入 getter ,也会通过 context 对象的属性传入 action 。

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