• main.js
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex)
  4. import store from './store'
  5. import App from './App'
  6. //挂载
  7. const app = new Vue({
  8. el: '#app',
  9. router,
  10. store,
  11. data: {
  12. },
  13. components: {
  14. App
  15. },
  16. render: h => h(App)
  17. })
  • store.js
  1. import Vuex from 'vuex'
  2. imoprt Vue from 'vue'
  3. Vue.use(Vuex);
  4. export default new Vuex.Store({
  5. state:{
  6. user:{}
  7. }
  8. })

踩过的坑

store提交的多种方式

  1. store.commit('fn', 'bar');
  2. store.commit('fn', {
  3. name: 'bar'
  4. })
  5. store.commit({
  6. type: 'fn',
  7. name: 'bar'
  8. })
  9. //默认情况下,每个提交过的 mutation 都会被发送到插件(如 devtools)
  10. //如果不希望插件去记录每个状态更改可以这样
  11. store.commit('increment',
  12. { amount: 1 },
  13. { silent: true }
  14. )
  15. store.commit({
  16. type: 'increment',
  17. amount: 1
  18. }, { silent: true })