Vuex

Vuex简介

官方解释:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。可以想象为一个“前端数据库”(数据仓库),

让其在各个页面上实现数据的共享包括状态,并且可操作

Vuex分成五个部分:
1.State:单一状态树

2.Getters:状态获取

3.Mutations:触发同步事件

4.Actions:提交mutation,可以包含异步操作

5.Module:将vuex进行分模块

vuex使用步骤

安装

在命令窗口输入以下代码
npm install vuex -S
创建store模块,分别维护state/actions/mutations/getters

store

index.js
state.js
actions.js
mutations.js
getters.js

文件目录
在src根目录下创建
在这里插入图片描述
在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实例

 import store from './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'
import 'element-ui/lib/theme-chalk/index.css'
/* process.env.MOCK && require('@/mock') */
import App from './App'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import axios from '@/api/http' 
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的核心概念:store、state、getters、mutations、actions

store

  每一个Vuex应用的核心就是store(仓库),store基本上就是一个容器,它包含着你的应用中大部分的状态 (state)。
  const store = new Vuex.Store({
   state,    // 共同维护的一个状态,state里面可以是很多个全局状态
   getters,  // 获取数据并渲染
   actions,  // 数据的异步操作
   mutations  // 处理数据的唯一途径,state的改变或赋值只能在这里
  })

state(保存数据的容器)

状态,即要全局读写的数据

 export default {
	resturantName: '猪肉餐馆'

}

getters(getXxx)

获取数据并渲染,

   export default {
	getResturantName: (state) => {
		return state.resturantName;
	}
}

getters将state中定义的值暴露在this.$store.getters对象中,我们可以通过如下代码访问

  this.$store.getters.resturantName

mutations(setXxx)

处理数据的唯一途径,state的改变或赋值只能在这里

export default {
	setResturantName: (state, payload) => {
		state.resturantName = payload.resturantName;
	}
}

mutations中方法的调用方式

不能直接调用this.$store.mutations.setResturantName('KFC'),必须使用如下方式调用:

   this.$store.commit(type,payload);


   // 1、把载荷和type分开提交

   store.commit('setResturantName',{

     resturantName:'KFC'

   })

actions

  数据的异步(async)操作
  如何理解javascript中的异步和同步?附录四:同步和异步的示例代码

demo

VuexPage1.vue

<template>
	<div>
		<h3>第一个vuex页面:{{title}}</h3>
		<button @click="changTitle">换人物了</button>
		<button @click="changTitleAsync">4秒后换人物了</button>
		<button @click="doAjax">测试vuex中使用ajax</button>
	</div>
</template>

<script>
	export default {
		data() {
			return {

			};
		},
		methods: {
			changTitle() {
				this.$store.commit('setResturantName',{
					resturantName:'羊肉饭馆'
				});
			},
			changTitleAsync(){
				this.$store.dispatch('setResturantNameAsync',{
					resturantName:'狗肉饭馆'
				});
			},
			doAjax(){
				this.$store.dispatch('doAjax',{
					_this:this
				});
			}
		},
		computed: {
			title() {
				// return this.$store.state.resturantName;
				return this.$store.getters.getResturantName;
			}
		}
	}
</script>

<style>

</style>

VuexPage2.vue

<template>
	<div>
		<h3>第二个vuex页面:{{title}}</h3>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				title:''
			};
		},
		created(){
			this.title= this.$store.state.resturantName;
		}
	}
</script>

<style>

</style>

actions.js

export default {
	setResturantNameAsync: (contex, payload) => {
		console.log('xxxx')
		setTimeout(() => {
			console.log('aaa')
			contex.commit('setResturantName', payload);
		}, 4000)
		console.log('bbb')
	},
	doAjax: (contex, payload) => {
		//vuex是不能使用Vue实例的
		let _this = payload._this;
		let url = _this.axios.urls.SYSTEM_USER_DOLOGIN;
		//get请求
		_this.axios.get(url, {}).then((response) => { //成功的回调函数
			console.log('ajax-------')
			console.log(response);
		}).catch((response) => { //失败的回调函数(异常)
			console.log(response);
		});
	}
}

getters.js

export default {
	getResturantName: (state) => {
		return state.resturantName;
	}
}

index.js

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

mutations.js

export default {
	setResturantName: (state, payload) => {
		state.resturantName = payload.resturantName;
	}
}

state.js

export default {
	resturantName: '猪肉餐馆'
	
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值