VUEX

拥有回忆 提交于 2019-12-06 23:13:54

1.vue中各个组件之间传值

1.父子组件
父组件–>子组件,通过子组件的自定义属性:props
子组件–>父组件,通过自定义事件:this.$emit(‘事件名’,参数1,参数2,…);

2.非父子组件或父子组件
通过数据总数Bus,this.root.root.root.emit(‘事件名’,参数1,参数2,…)

3.非父子组件或父子组件
更好的方式是在vue中使用vuex

方法1: 用组件之间通讯。这样写很麻烦,并且写着写着,估计自己都不知道这是啥了,很容易写晕。
方法2: 我们定义全局变量。模块a的数据赋值给全局变量x。然后模块b获取x。这样我们就很容易获取到数据

2.了解vuex中的各个js文件的用途

在这里插入图片描述
官方解释:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。可以想象为一个“前端数据库”(数据仓库), 让其在各个页面上实现数据的共享包括状态,并且可操作

Vuex分成五个部分:
1.State:单一状态树
2.Getters:状态获取
3.Mutations:触发同步事件
4.Actions:提交mutation,可以包含异步操作
5.Module:将vuex进行分模块

3. vuex使用步骤

1.安装vuex

在对应的vue工程里打开命令框 输入
npm install vuex -S
等待下载完成,查看package.json文件中就可看到vuex的版本号

vuex下载安装好了。

2.在src目录下创建store模块,分别维护

state/actions/mutations/getters 和 index.js

在这里插入图片描述

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

4.vuex取值:

1.先来两个组件,并且在路由index中配置
在这里插入图片描述
index

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Login from '@/components/Login'
import Reg from '@/components/Reg'
import AppMain from '@/components/AppMain'
// import LeftNav from '@/components/LeftNav'
// import TopNav from '@/components/TopNav'
import Articles from '@/components/sys/Articles'

import VuexPage1 from '@/components/sys/VuexPage1'
import VuexPage2 from '@/components/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: '/sys/Articles',
							name: 'Articles',
							component: Articles,
						},
						{
							path: '/sys/VuexPage1',
							name: 'VuexPage1',
							component: VuexPage1
						},
						{
							path: '/sys/VuexPage2',
							name: 'VuexPage2',
							component: VuexPage2
						}
					],
					}
					]
			})

2、在state中存放一个变量拿来测试
state.js

export default{
	resturantName:'小厨娘'
}

3.取值的两种方式:
①直接取值:VuexPage1


<template>
	<div>
		第一章页面:{{msg}}
	</div>
</template>

<script>
	export default {
		data() {
			return {
				
			};
		},
		computed: {
			msg() {//不建议
				return this.$store.state.resturantName;
			}
		},
	}
</script>

<style>

</style>

在这里插入图片描述
②将state中定义的值暴露在this.$store.getters对象中

getters

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

VuexPage2:

<template>
	<div>
		第二章页面:{{msg}}
	</div>
</template>

<script>
	export default {
		data() {
			return {
				
			};
		},
		computed: {
			msg() {
				return this.$store.getters.getResturantName;
			}
		},
	}
</script>

<style>

</style>


在这里插入图片描述

5.vuex存值

①同步存值:

Mutations.js

export default{//payload 载荷 保存参数的容器
	setResturantName:(state,payload)=>{
		state.resturantName=payload.resturantName;
	}
}

②异步:
Action.js

export default{//context相当于上下文
	setResturantNamePlus:(context,payload)=>{
		console.log("异步1")
		setTimeout(()=>{
			console.log("异步2")
			context.commit('setResturantName',{
				ResturantName:payload.resturantName
			})
	
	},6000)//延迟6000秒
	console.log("异步3")
}
}

VuexPage1:

<template>
	<div>
		第一章页面:{{msg}}<br />
		<input v-model="newName" />
		<button @click="buy">同步存值</button>
		<button @click="badbuy">异步存值</button>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				newName:''
			};
		},
		computed: {//计算属性  :state状态存储是响应式的,从store实例中读取状态最简单的方法就是在计算属性中返回某个状态
			msg() {//不建议
				return this.$store.state.resturantName;
			}
		},
		methods: {
			buy() {//同步
				this.$store.commit('setResturantName',{
					resturantName:this.newName
				})
			},
			badbuy(){//异步
				this.$store.dispatch('setResturantNamePlus',{
					resturantName:this.newName
				})
			}
		},
	}
</script>

<style>

</style>


不管是同步还是异步,在修改值的同时,其他组件内的值也会跟着改变

6.调用ajax

由于无法直接获得this 所以无法直接使用 this.axios 这时候需要我们从组件里传递过来。

Action.js

export default { //context相当于上下文
	setResturantNameAsync: (context, payload) => {
		console.log("异步1")
		setTimeout(() => {
			console.log("异步2")
			context.commit('setResturantName', {
			resturantName : payload.resturantName
			})
		}, 6000)//延迟6秒
		console.log("异步3")
	},
	doAjax:(context,payload)=>{
		//这里面无法直接获取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);
		});
	}
}


VuexPage1:

<template>
	<div>
		第一章页面:{{msg}}<br />
		<input v-model="newName" />
		<button @click="buy">同步存值</button>
		<button @click="badbuy">异步存值</button>
		<button @click="doAjax">调用ajax</button>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				newName: ''
			};
		},
		computed: { //计算属性  :state状态存储是响应式的,从store实例中读取状态最简单的方法就是在计算属性中返回某个状态
			msg() { //不建议
				return this.$store.state.resturantName;
			}
		},
		methods: {
			buy() { //同步
				this.$store.commit('setResturantName', {
					resturantName: this.newName
				})
			},
			badbuy() { //异步
				this.$store.dispatch('setResturantNamePlus', {
					resturantName: this.newName
				})
			},
			doAjax() { //为了在异步里使用ajax将this传递过去
				this.$store.dispatch('doAjax', {
					_this: this
				})

			}
		},
	}
</script>

<style>

</style>

在这里插入图片描述

在这里插入图片描述

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!