Vuex

vue中各个组件之间传值

  1. 父子组件

    父组件–>子组件,通过子组件的自定义属性:props

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

  2. 非父子组件或父子组件

    通过数据总数Bus,this.$ root.$emit(‘事件名’,参数1,参数2,…)

  3. 非父子组件或父子组件

    更好的方式是在vue中使用vuex

方法1: 用组件之间通讯。这样写很麻烦,并且写着写着,估计自己都不知道这是啥了,很容易写晕。

方法2: 我们定义全局变量。模块a的数据赋值给全局变量x。然后模块b获取x。这样我们就很容易获取到数据

Vuex

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

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

Vuex分成五个部分:

1.State:单一状态树

2.Getters:状态获取

3.Mutations:触发同步事件

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

5.Module:将vuex进行分模块

在这里插入图片描述

vuex使用步骤

  1. 安装

    npm install vuex -S
    在这里插入图片描述

  2. 创建store模块,分别维护state/actions/mutations/getters

    store

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

在这里插入图片描述

  1. 在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
  1. 在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的改变或赋值只能在这里

  })

state(保存数据的容器)

状态,即要全局读写的数据
state.js:
在这里插入图片描述

getters

获取数据并渲染
注1:getters将state中定义的值暴露在this.$store.getters对象中,我们可以通过如下代码访问

      this.$store.getters.resturantName

注2:state状态存储是响应式的,从store实例中读取状态最简单的方法就是在计算属性中返回某个状态
getters.js:
在这里插入图片描述

mutations

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

payload:官方给它还取了一个高大上的名字:载荷,其实就是一个保存要传递参数的容器
在这里插入图片描述
mutations中方法的调用方式

不能直接调用this.$ store.mutations.setResturantName(’’),必须使用如下方式调用:
this.$store.commit(type,payload);

VuexPage1.vue:

<template>
	<div>
		<h3>用户管理页面的:{{title}}</h3>
		<button @click="Change">改变</button>
	</div>
</template>

<script>
	export default{
		data(){
			return{
				
			}
		},
		methods:{
			Change(){
				this.$store.commit('setResturantName',{
					resturantName:'麻婆豆腐'
				})
			}
		},
		computed:{
			title(){
				// return this.$store.state.resturantName;
				return this.$store.getters.getResturantName;
			}
		}
	}
</script>

<style>
</style>

VuexPage2.vue:

<template>
	<div>
		<h3>角色管理页面的:{{title}}</h3>
	</div>
</template>

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

<style>
</style>

在这里插入图片描述
在这里插入图片描述
点击改变后:
在这里插入图片描述
在这里插入图片描述
这就是vuex维护公共状态或数据的魅力,在一个地方修改了数据,在这个项目的其他页面都会变成这个数据。
Mutation 必须是同步函数。为什么呢?异步方法,我们不知道什么时候状态会发生改变,所以也就无法追踪了
如果我们需要异步操作,Mutations就不能满足我们需求了,这时候我们就需要Actions了

actions

数据的异步(async)操作

Action类似于 mutation,不同在于:

1.Action提交的是mutation,而不是直接变更状态

2.Action可以包含任意异步操作

3.Action的回调函数接收一个 context 上下文参数,注意,这个参数可不一般,它与 store 实例有着相同的方法和属性
但是他们并不是同一个实例,context 包含:
1. state、2. rootState、3. getters、4. mutations、5. actions 五个属性
所以在这里可以使用 context.commit 来提交一个 mutation,或者通过 context.state 和 context.getters 来获取 state 和 getters。
在这里插入图片描述

ChangeAsync(){
				this.$store.dispatch('setResturantNameByAsync',{
					resturantName:'麻辣小龙虾'
				})
			}

在这里插入图片描述
在这里插入图片描述
测试使用ajax:
在这里插入图片描述

			doAjax(){
				this.$store.dispatch('doAjax',{
					_this:this
				})
			}

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值