2017-09-14 99 views
0

我想使用Vuex来为不使用Vue的服务器端应用程序提供支持。这可能吗?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) 

回答

-1

彼时我加入的Vue,而无需创建Vue公司的应用程序:

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

我的小测试商店现在的作品。我不知道如果没有创建Vue应用程序Vue.use(Vuex)会导致任何问题。

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

相关问题