首先,我们要安装vuex,执行命令yarn add vuex
1.编写state数据
//vuex/index.js
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
const state = {
bookList: [
{id: 1, name: '西游记'},{id: 2, name: '水浒传'},
{id: 3, name: '红楼梦'},{id: 4, name: '三国演义'}
]
}
export default new Vuex.Store({
state
})
2.组件调用
1