目录
3、在store/index.js文件中新建vuex的store实例
一、Vuex中的各个js文件的用途
1、变量传值的演变形式
2、Vuex各组件
3、官方图解Vuex
4. Vuex
官方解释:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。可以想象为一个“前端数据库”(数据仓库), 让其在各个页面上实现数据的共享包括状态,并且可操作
Vuex分成五个部分:
1.State:单一状态树
2.Getters:状态获取
3.Mutations:触发同步事件
4.Actions:提交mutation,可以包含异步操作
5.Module:将vuex进行分模块
二、 vuex使用步骤
1、 安装Vuex
npm install vuex -S
2、创建store模块
分别维护state/actions/mutations/getters
state, //共同维护的一个状态,state里面可以是很多个全局状态
getters,//获取数据并渲染
actions,// 数据的异步操作
mutations,//处理数据的唯一途径,state的改变或赋值只能在这里
3、在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
4、在main.js中导入并使用store实例
三、利用vuex取值
1、State.js
export default{
resturantName:'是小黑'
}
2、VuexPage1.vue
<template>
<div>
<h3>页面1:小白{{msg}}</h3>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
}
},
methods:{
panta(){
})
}
},
computed:{
msg(){
// return "白"
return this.$store.state.resturantName
}
}
}
</script>
<style>
</style>
3、VuexPage2.vue
<template>
<div>
<h3>页面2:小白{{msg}}</h3>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
}
},
computed:{
msg(){
return this.$store.state.resturantName
}
}
}
</script>
<style>
</style>
4、配置路由
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Login from '@/views/Login'
import Reg from '@/views/Reg'
import AppMain from '@/components/AppMain'
import LeftNav from '@/components/LeftNav'
import TopNav from '@/components/TopNav'
import Articles from '@/views/sys/Articles'
import VuexPage1 from '@/views/sys/VuexPage1'
import VuexPage2 from '@/views/sys/VuexPage2'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: '/LeftNav',
name: 'LeftNav',
component: LeftNav
}, {
path: '/TopNav',
name: 'TopNav',
component: TopNav
}, {
path: '/sys/Articles',
name: 'Articles',
component: Articles
}, {
path: '/sys/VuexPage1',
name: 'VuexPage1',
component: VuexPage1
}, {
path: '/sys/VuexPage2',
name: 'VuexPage2',
component: VuexPage2
}]
}]
})
5、运行结果
return this.$store.state.resturantName;
这个取值方法对类而言,安全性和封装性不高,
所以不推荐
6、使用getters取值
①、Getters.js
export default{
getResturantName:(state)=>{
return state.resturantName
}
}
②、修改VuexPage1.vue
computed:{
msg(){
// return "白"
// return this.$store.state.resturantName
return this.$store.getters.getResturantName;
}
}
结果与上面一样
四、Vuex存值
1、Mutations.js
payload:官方给它还取了一个高大上的名字:载荷,其实就是一个保存要传递参数的容器
一定要记住,Mutation 必须是同步函数。为什么呢?异步方法,我们不知道什么时候状态会发生改变,所以也就无法追踪了
如果我们需要异步操作,Mutations就不能满足我们需求了,这时候我们就需要Actions了
export default{
setResturantName:(state,payload)=>{
state.resturantName=payload.resturantName;
},
}
2、修改VuexPage1.vue
<template>
<div>
<h3>页面1:小白{{msg}}</h3>
<button @click="panta">点我</button>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
}
},
methods:{
panta(){
this.$store.commit("setResturantName",{
resturantName:"不是小黑"
})
}
},
computed:{
msg(){
// return "白"
// return this.$store.state.resturantName
return this.$store.getters.getResturantName;
}
}
}
</script>
<style>
</style>
3、运行结果
点击VuexPage1点我的按钮时内容改变,同时VuexPage2也会跟着改变
①、未点击
②、已点击
五、Vuex的异步加载问题及后台调用问题
1、数据的异步操作
Action类似于 mutation,不同在于:
(1)Action提交的是mutation,而不是直接变更状态
(2)Action可以包含任意异步操作
(3)Action的回调函数接收一个 context 上下文参数,注意,这个参数可不一般,它与 store 实例有着相同的方法和属性,但是他们不是同一个实例
①、Actions.js
export default{
setResturantNameAsync:(context,payload)=>{
// contex等价于this.¥store,也就是它代表了Vuex的上下文
// 在这个文件中是可以调用同步文件mutations.js定义的同步方法
// state.resturantName=payload.resturantName;
setTimeout(function() {
context.commit("setResturantName",payload);
},6000);
}
}
②、VuexPage1.vue
通过dispatch调用Actions.js里面的setResturantNameAsync方法
<template>
<div>
<h3>页面1:小白{{msg}}</h3>
<button @click="panta">点我</button>
<button @click="pantaAsync">点它</button>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
}
},
methods:{
panta(){
this.$store.commit("setResturantName",{
resturantName:"不是小黑"
})
},
pantaAsync(){
this.$store.dispatch("setResturantNameAsync",{
resturantName:"是小黑"
})
}
},
computed:{
msg(){
// return "白"
// return this.$store.state.resturantName
return this.$store.getters.getResturantName;
}
}
}
</script>
<style>
</style>
③、运行结果
(1)、点击点我
(2)、点击点它,6秒后,结果改变
2、Vuex与后台数据交互问题
①、Mutations.js
export default{
setResturantName:(state,payload)=>{
state.resturantName=payload.resturantName;
},
doAjax:(state,payload)=>{
// 需求:想在当前文件中与后台服务器做数据交互
let url = this.axios.urls.SYSTEM_MENU_TREE;
// 箭头函数解决了this 指针污染的问题
this.axios.post(url, {}).then((resp) => {
console.log(resp);
}).catch(function(error) {
console.log(error);
});
}
}
②、VuexPage1.vue
<template>
<div>
<h3>页面1:小白{{msg}}</h3>
<button @click="panta">点我</button>
<button @click="pantaAsync">点它</button>
<button @click="doAjax">Vuex与后台交互</button>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
}
},
methods:{
panta(){
this.$store.commit("setResturantName",{
resturantName:"不是小黑"
})
},
doAjax(){
})
},
pantaAsync(){
this.$store.dispatch("setResturantNameAsync",{
resturantName:"是小黑"
})
}
},
computed:{
msg(){
// return "白"
// return this.$store.state.resturantName
return this.$store.getters.getResturantName;
}
}
}
</script>
<style>
</style>
③、点击ajax与后台交互则会报错
④、解决方法
(1)、修改vuexpage1.vue
methods:{
doAjax(){
this.$store.commit("doAjax",{
_this:this
})
}
},
(3)、修改Mutations.js
export default{
setResturantName:(state,payload)=>{
state.resturantName=payload.resturantName;
},
doAjax:(state,payload)=>{
// 需求:想在当前文件中与后台服务器做数据交互
let _this=payload._this;
let url = _this.axios.urls.SYSTEM_MENU_TREE;
// 箭头函数解决了this 指针污染的问题
_this.axios.post(url, {}).then((resp) => {
console.log(resp);
}).catch(function(error) {
console.log(error);
});
}
}
(3)、运行结果