Skip to content

[vue] 你有写过vuex中store的插件吗? #539

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

[vue] 你有写过vuex中store的插件吗?

Activity

lostimever

lostimever commented on Mar 10, 2020

@lostimever

Vuexstore 接受 plugins 选项,这个选项暴露出每次 mutation 的钩子。Vuex 插件就是一个函数,它接收 store 作为唯一参数:

const myPlugin = store => {
  // 当 store 初始化后调用
  store.subscribe((mutation, state) => {
    // 每次 mutation 之后调用
    // mutation 的格式为 { type, payload }
  });
};

然后像这样使用:

const store = new Vuex.Store({
  // ...
  plugins: [myPlugin]
});

官方文档

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @haizhilin2013@lostimever

        Issue actions

          [vue] 你有写过vuex中store的插件吗? · Issue #539 · haizlin/fe-interview