首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vuex没有Vue?

Vuex没有Vue?
EN

Stack Overflow用户
提问于 2017-09-14 18:36:41
回答 1查看 743关注 0票数 0

我想使用Vuex来驱动不使用Vue的服务器端应用程序。这个是可能的吗?

代码语言:javascript
复制
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");

以下是我遇到的错误:

代码语言:javascript
复制
$ 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)
EN

回答 1

Stack Overflow用户

发布于 2017-09-15 15:20:24

最后,我添加了Vue,而没有创建Vue应用程序:

代码语言:javascript
复制
const Vue = require('vue');
Vue.use(Vuex);

我的小测试店现在可以用了。我不知道如果不创建Vue应用程序,Vue.use(Vuex)是否会引起任何问题。

值得注意的是,对于服务器端用例,Vuex并不会在每次提交时调用我的观察者;它会批次更改并调用观察者一次。我没看到这份文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46226148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档