vuex文件
// store.js
import Vue from 'vue'
import Vuex from 'vuex'
import actions from './actions.js'
import ImageInfo from './modules/imageInfo.js'
Vue.use(Vuex)
const store = new Vuex.Store({
state:{
type:['Video','Image'],
videoList:[],
imageList:[]
},
actions,
modules:{
ImageInfo
}
})
actions文件
// actions.js文件
export default {
async getVideoList({rootState}){
const { data : rootState.videoList } = await ...
......
}
async getRecommendList({rootState,dispatch}){
......
const intefaceName = `get${rootState.type[0]}List`
dispacth(intefaceName) //可以传常量/变量(类型:字符串)
......
// 方法前必须加上模块名称
dispacth(`ImageInfo/get${rootState.type[1]}List`)
......
}
}
如果调用的是module中的方法,必须加模块名称
modules文件夹:
// imageInfo.js
export default {
namespaced: true,
actions:{
async getImageList({rootState}){
const { data : rootState.imageList } = await ...
......
}
}
}