main.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import store from './store'
import App from './App'
//挂载
const app = new Vue({
el: '#app',
router,
store,
data: {
},
components: {
App
},
render: h => h(App)
})
store.js
import Vuex from 'vuex'
imoprt Vue from 'vue'
Vue.use(Vuex);
export default new Vuex.Store({
state:{
user:{}
}
})
踩过的坑
store提交的多种方式
store.commit('fn', 'bar');
store.commit('fn', {
name: 'bar'
})
store.commit({
type: 'fn',
name: 'bar'
})
//默认情况下,每个提交过的 mutation 都会被发送到插件(如 devtools)
//如果不希望插件去记录每个状态更改可以这样
store.commit('increment',
{ amount: 1 },
{ silent: true }
)
store.commit({
type: 'increment',
amount: 1
}, { silent: true })