课程目标
一、了解vuex中的各个js文件的用途
二、利用vuex存值
三、利用vuex取值
四、Vuex的异步加载问题
一、了解vuex中的各个js文件的用途
变量传值的演变形式
图解vuex
使用步骤
-
安装
npm install vuex -S -
创建store模块,分别维护state/actions/mutations/getters
-
在store/index.js文件中新建vuex的store实例,并注册上面引入的各大模块
import Vue from 'vue'
import Vuex from 'vuex'
import state from './state'
import getters from './getters'
import actions from './actions'
import mutations from './mutations'
Vue.use(Vuex)
const store = new Vuex.Store({
state,
getters,
actions,
mutations
})
export default store
- 在main.js中导入并使用store实例
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
// process.env.MOCK && require('@/mock')
import 'element-ui/lib/theme-chalk/index.css'
import App from './App'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import axios from '@/api/http'
// import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(ElementUI)
Vue.use(VueAxios,axios)
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
data(){
return{
Bus:new Vue({
})
}
},
router,
store,
components: { App },
template: '<App/>'
})
二、利用vuex存、取值
VuexPage1.vue
<template>
<div>
<ul>
<li>这是第一张页面:{{msg}}</li>
<li>
<input v-model="newName" />
<button @click="buy">盘它</button>
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
newName:''
};
},
computed:{
msg(){
//不推荐,因为vuex遵循了编码解耦的原则,各模块各司其职
return this.$store.state.resturantName;
}
},
methods:{
buy(){
console.log(this.newName);
this.$store.commit('setResturantName',{
resturantName:this.newName
})
}
}
}
</script>
<style>
</style>
VuexPage2.vue
<template>
<div>
<ul>
<li>这是第二张页面:{{msg}}</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
};
},
computed:{
msg(){
return this.$store.getters.getResturantName;
}
}
}
</script>
<style>
</style>
添加路由index.js
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import AppMain from '@/components/AppMain'
import Articles from '@/views/sys/Articles'
import VuexPage1 from '@/views/sys/VuexPage1'
import VuexPage2 from '@/views/sys/VuexPage2'
import Login from '@/views/Login'
import Reg from '@/views/Reg'
Vue.use(Router)
export default new Router({
routes: [{
path: '/',
name: 'Login',
component: Login
},
{
path: '/Login',
name: 'Login',
component: Login
},
{
path: '/Reg',
name: 'Reg',
component: Reg
},
{
path: '/AppMain',
name: 'AppMain',
component: AppMain,
children: [{
path: '/sys/Articles',
name: 'Articles',
component: Articles
},
{
path: '/sys/VuexPage1',
name: 'VuexPage1',
component: VuexPage1
},
{
path: '/sys/VuexPage2',
name: 'VuexPage2',
component: VuexPage2
}
]
}
]
})
state.js
export default{
resturantName:'飞歌餐馆'
}
getters.js
export default{
getResturantName:(state)=>{
return state.resturantName;
}
}
mutations.js
export default{
setResturantName:(state,payload)=>{
state.resturantName=payload.resturantName
}
}
显示效果
四、Vuex的异步加载问题
VuexPage1.vue
<template>
<div>
<ul>
<li>这是第一张页面:{{msg}}</li>
<li>
<input v-model="newName" />
<button @click="buy">盘它</button>
<button @click="badBuy">有阴谋</button>
<button @click="doAjax">调用ajax</button>
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
newName:''
};
},
computed:{
msg(){
//不推荐,因为vuex遵循了编码解耦的原则,各模块各司其职
return this.$store.state.resturantName;
}
},
methods:{
buy(){
console.log(this.newName);
this.$store.commit('setResturantName',{
resturantName:this.newName
})
},
badBuy(){
this.$store.dispatch('setResturantNameAsync',{
resturantName:this.newName
})
},
doAjax(){
this.$store.dispatch('doAjax',{
_this:this
})
}
}
}
</script>
<style>
</style>
actions.js
export default{
setResturantNameAsync:(context,payload)=>{
console.log('xxx');
setTimeout(()=>{
console.log('yyy');
context.commit('setResturantName',{
resturantName:payload.resturantName
})
},6000);
console.log('zzz');
},
doAjax:(context,payload)=>{
//this.axios 不能用this,获取不到
let _this=payload._this;
let url = _this.axios.urls.SYSTEM_MENU_TREE;
_this.axios.post(url, {}).then((response) => {
console.log(response);
}).catch(function(error) {
console.log(error);
});
}
}
显示效果