【问题标题】:Vuex without Vue?没有 Vue 的 Vuex?
【发布时间】:2018-02-23 20:29:23
【问题描述】:

我想使用 Vuex 来支持不使用 Vue 的服务器端应用程序。这可能吗?

const Vuex = require('vuex');

const store = new Vuex.Store({
    state: {
        potatoes: 1,
    },
    getters: {
        doublePotatoes(state) {
            return state.potatoes * 2;
        },
    },
    mutations: {
        addPotato(state) {
            state.potatoes += 1;
        },
    }
});

store.watch((state, getters) => getters.doublePotatoes, console.log);
store.commit("addPotato");

这是我得到的错误:

$ node index.js
/private/tmp/vtest/node_modules/vuex/dist/vuex.common.js:99
if (!condition) { throw new Error(("[vuex] " + msg)) }
                    ^

Error: [vuex] must call Vue.use(Vuex) before creating a store instance.
    at assert (/private/tmp/vtest/node_modules/vuex/dist/vuex.common.js:99:27)
    at new Store (/private/tmp/vtest/node_modules/vuex/dist/vuex.common.js:279:5)
    at Object.<anonymous> (/private/tmp/vtest/index.js:3:15)
    at Module._compile (module.js:573:30)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Function.Module.runMain (module.js:609:10)
    at startup (bootstrap_node.js:158:16)

【问题讨论】:

    标签: node.js vuex


    【解决方案1】:

    我在没有创建 Vue 应用的情况下添加了 Vue:

    const Vue = require('vue');
    Vue.use(Vuex);
    

    我的小测试商店现在可以工作了。不知道Vue.use(Vuex)不创建Vue应用会不会出问题。

    值得注意的是,对于服务器端用例,Vuex 不会为 每个 提交调用我的观察者;它批量更改并调用观察者一次。我没有看到这个记录。

    【讨论】:

    • 这对您来说效果如何?我喜欢你的风格!状态管理也是服务器应用程序的一个问题。
    猜你喜欢
    • 2018-06-02
    • 2021-02-10
    • 2021-07-05
    • 2021-01-26
    • 1970-01-01
    • 2018-09-15
    • 2019-08-06
    • 2019-06-01
    • 2020-08-21
    相关资源
    最近更新 更多