programing

vuex에 여러 플러그인을 등록하는 방법?

prostudy 2022. 5. 24. 21:52
반응형

vuex에 여러 플러그인을 등록하는 방법?

나는 이미 내 프로젝트에서 vue-persistedstate 플러그인을 사용하고 있었다.이제 나는 vuex-pathify 플러그인도 필요하다.

영구 상태의 경우 설정을 포함한 상수를 준비한다.

const persistedStates = [
    createPersistedState({
        key: process.env.PERSISTENT_COOKIE_NAME,
        paths: [
            'auth'
        ],
        storage: {
            getItem: key => JSON.stringify(cookies.get(key)),
            setItem: (key, value) => cookies.set(key, value, { path: '/', expires: '6h', secure: false }),
            removeItem: key => cookies.remove(key)
        },
    }),
    createPersistedState({
        key: process.env.PERSISTENT_COOKIE_NAME,
        paths: [
            'user',
        ],
    })
];

그런 다음 다음과 같이 사용하십시오.

const Store = new Vuex.Store({
    ...
    plugin: persistedStates;
    ...
})

이제 pathify 플러그인도 추가해야겠어.나는 노력했다.

plugin: [persistedStates, pathify.plugin]

그러나 작동하지 않고 오류를 반환함(vuex.esm.js?94e4:368 Uncaught (in promise) TypeError: plugin is not a function내가 뭘 놓쳤지?

Vuex doc은 매우 직설적이다: https://vuex.vuejs.org/guide/plugins.html

const myPlugin = store => {
  // called when the store is initialized
  store.subscribe((mutation, state) => {
    // called after every mutation.
    // The mutation comes in the format of `{ type, payload }`.
  })
}

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

참조URL: https://stackoverflow.com/questions/63437078/how-to-register-multiple-plugins-in-vuex

반응형