import Vue from 'vue'
import VueRouter from 'vue-router'
// import store from './store/store'
import Vuex from 'vuex'
import createStore from './store/store'
vue.use(VueRouter)
Vue.use(Vuex)
const router = createRouter()
const store = createStore()
new Vue({
router,
store,
render: (h) => h(App)
}).$mount('#root')
Vuex
import Vuex from 'vuex' import Vue from 'vue'
Vue.use(Vuex)
// 会缓存全局唯一的store,服务端渲染会内存溢出 const store = new Vuex.Store({ // 初始化store state: { count: 0 },
})
export default store
mounted(){ console.log(this.$store) // 应用入口传入的store对象
}
computed: { count(){ // 获得count return this.$store.state.count } }
import Vue from 'vue' import VueRouter from 'vue-router' // import store from './store/store' import Vuex from 'vuex' import createStore from './store/store'
vue.use(VueRouter) Vue.use(Vuex)
const router = createRouter() const store = createStore()
new Vue({ router, store, render: (h) => h(App) }).$mount('#root')
重新组织,单模块的store
// 所有值都要事先声明,才能实现响应式 export default { count: 0 }
import Vuex from 'vuex'
// 导入默认state import defaultState from './state/state'
// 导入默认mutations import mutations from './mutations/mutations'
//服务端渲染用法,返回函数,防止内存溢出 export default () => { return new Vuex.Store({ // 初始化store // 单独的state,更好的组织state state: defaultState,
}
// getters类似computed属性 // 用于生成在应用内可直接使用的数据,方便实用,无需在computed中格式化,使代码可复用,易维护 // 用来将后端数据格式化为适合view层显示的数据 export default { fullName(state) { // 默认接受state return
${state.firstNmae} ${state.lastName}
} }import Vuex from 'vuex'
// 导入默认state import defaultState from './state/state'
// 导入默认mutations import mutations from './mutations/mutations'
// 导入默认的getters import getters from './getters/getters'
//服务端渲染用法,返回函数,防止内存溢出 export default () => { return new Vuex.Store({ // 初始化store // 单独的state,更好的组织state state: defaultState,
}
mounted(){ console.log(this.$store) // 应用入口传入的store对象
}
computed: { count(){ // 获得count return this.$store.state.count }, fullName(){ return this.$store.getters.fullName } }
Vuex : mutation,action
import Vuex from 'vuex'
// 导入默认state import defaultState from './state/state'
// 导入默认mutations import mutations from './mutations/mutations'
// 导入默认的getters import getters from './getters/getters'
const isDev = process.env.NODE_ENV === 'development'
//服务端渲染用法,返回函数,防止内存溢出 export default () => { return new Vuex.Store({ // 初始化store
}
import Vuex from 'vuex'
// 导入默认state import defaultState from './state/state'
// 导入默认mutations import mutations from './mutations/mutations'
// 导入默认的getters import getters from './getters/getters'
// 导入默认的actions import actions from './actions/actions'
const isDev = process.env.NODE_ENV === 'development'
//服务端渲染用法,返回函数,防止内存溢出 export default () => { return new Vuex.Store({ // 初始化store
}
// Actions,Mutations 帮助函数 import { mapState, mapGetters, mapActions, // 对应异步操作 mapMutations // 对应同步操作 } from 'vuex'
methods: { ...mapActions(['updateCountAsync']), ...mapMutations(['updateCount']) }
mounted(){ // 提交action必须使用 this.$store.dispatch // this.$store.dispatch( // 'updateCountAsync', // action的方法名字面量 // { // // 提交action时传入的参数对象 // num: 5, // time: 2000, // } // )
}
{{textA}}
computed:{ // 模块后,带命名空间的调用 textA(){ return this.$store.state.a.text } }
import Vuex from 'vuex'
// 导入默认state import defaultState from './state/state'
// 导入默认mutations import mutations from './mutations/mutations'
// 导入默认的getters import getters from './getters/getters'
// 导入默认的actions import actions from './actions/actions'
const isDev = process.env.NODE_ENV === 'development'
//服务端渲染用法,返回函数,防止内存溢出 export default () => { return new Vuex.Store({ // 初始化store
}
import Vuex from 'vuex'
// 导入默认state import defaultState from './state/state'
// 导入默认mutations import mutations from './mutations/mutations'
// 导入默认的getters import getters from './getters/getters'
// 导入默认的actions import actions from './actions/actions'
const isDev = process.env.NODE_ENV === 'development'
//服务端渲染用法,返回函数,防止内存溢出 export default () => { return new Vuex.Store({ // 初始化store
}
import Vuex from 'vuex'
// 导入默认state import defaultState from './state/state'
// 导入默认mutations import mutations from './mutations/mutations'
// 导入默认的getters import getters from './getters/getters'
// 导入默认的actions import actions from './actions/actions'
const isDev = process.env.NODE_ENV === 'development'
//服务端渲染用法,返回函数,防止内存溢出 export default () => { return new Vuex.Store({ // 初始化store
}
{{ textA }}
// Actions,Mutations 帮助函数 import { mapState, mapGetters, mapActions, // 对应异步操作 mapMutations // 对应同步操作 } from 'vuex'
methods: { ...mapActions(['updateCountAsync']), ...mapMutations(['updateCount', 'a/updateText']), // 帮助函数声明模块化的 updateText mutation // Vuex 默认会将全部mutation,包括模块化的,放入全局mutations // 启用命名空间后,调用模块内的mutations --- a/updateText },
mounted(){
},
computed: { // 模块后,带命名空间的调用 // textA(){ // return this.$store.state.a.text // }
}
{{ textA }}
// Actions,Mutations 帮助函数 import { mapState, mapGetters, mapActions, // 对应异步操作 mapMutations // 对应同步操作 } from 'vuex'
methods: { ...mapActions(['updateCountAsync', 'a/add', 'textAction']), // b模块未声明命名空间,textAction无需写模块名 ...mapMutations(['updateCount', 'a/updateText']), // 帮助函数声明模块化的 updateText mutation // Vuex 默认会将全部mutation,包括模块化的,放入全局mutations // 启用命名空间后,调用模块内的mutations --- a/updateText },
mounted(){ this['a/add']()
},
computed: { // 模块后,带命名空间的调用 // textA(){ // return this.$store.state.a.text // }
}
Vuex 热更新
import Vuex from 'vuex'
// 导入默认state import defaultState from './state/state'
// 导入默认mutations import mutations from './mutations/mutations'
// 导入默认的getters import getters from './getters/getters'
// 导入默认的actions import actions from './actions/actions'
const isDev = process.env.NODE_ENV === 'development'
//服务端渲染用法,返回函数,防止内存溢出 export default () => { const store = new Vuex.Store({ // 初始化store
}
)
import Vuex from 'vuex'
// 导入默认state import defaultState from './state/state'
// 导入默认mutations import mutations from './mutations/mutations'
// 导入默认的getters import getters from './getters/getters'
// 导入默认的actions import actions from './actions/actions'
const isDev = process.env.NODE_ENV === 'development'
//服务端渲染用法,返回函数,防止内存溢出 export default () => { const store = new Vuex.Store({ // 初始化store
}