- 新建文件
const state = {
zp: [],
postName: ''
}
// getters
const getters = {}
// actions
const actions = {}
// mutations
const mutations = {
setZp(state, zp) { // 设置参数
state.zp = zp
},
setPostName(state, postName) {
state.postName = postName
}
}
export default {
namespaced: true,
state,
getters,
actions,
mutations
}
- 挂载
import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'
// import zp from './modules/zpList'
Vue.use(Vuex)
...
const store = new Vuex.Store({
modules,
getters
})
export default store
- 存入
async search() {
if (this.zpList.postName) {
this.zpList = this.zpList.postName
}
const { data: res } = await zpList(JSON.stringify(this.zpList))
console.log(res)
this.$store.commit('zpList/setZp', { // zpList表示vuex的文件名
zp: res.data
})
if (res.code == '0') {
this.$message.success(res.msg)
}
},
4.使用
import { mapState } from 'vuex'
// 计算属性
computed: {
...mapState({
// 'zp'
zpList: state => state.zpList.zp
})
},
网友评论