创建store文件夹
1.功能:放异步操作
文件:actions.js
内容:
2.功能:获取state里数据
文件:getters.js
内容:
export const singer = state => state.singer
3.功能:放置方法名
文件:mutation-types.js
内容:
export const SET_SINGER = 'SET_SINGER'
4.功能:操作state数据
文件:mutations.js
内容:
import * as types from './mutation-types'
const mutation = {
[types.SET_SINGER](state,singer){
state.singer = singer
}
}
export default mutations
5.功能:数据
文件:state.js
内容:
const state = {
singer:{}
}
export default state
6.功能:入口
文件:index.js
内容:
import Vue from 'vue'
import Vuex from 'vuex'
import * as actions from './actions'
import * as getter from './getters'
import mutations from './mutations'
import state from './state'
import createLogger from 'vuex/dist/logger'
Vue.use(Vuex)
// 开发环境时,跟踪每次state的修改
const debug = process.env.NODE_ENV !== 'production'
export default new Vuex.Store({
actions,
getters,
mutations,
state,
strict: debug,
plugin: debug ? [createLogger()] : []
})
Atas ialah kandungan terperinci 实现vuex的初始化方法. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!