vuex:store传值/组件之间相互调用方法

赋值 this.$store.commit('add', 3);
异步赋值 this.$store.dispatch('addAsync')
计算属性取值 this.$store.getters.方法名
取值 this.$store.state.全局变量名称


var Event = new Vue();      相当于又new了一个vue实例,Event中含有vue的全部方法;
Event.$emit('msg',this.msg);      发送数据,第一个参数是发送数据的名称,接收时还用这个名字接收,第二个参数是这个数据现在的位置;

methods:{
			getLogin(){
				uni.$emit('play-video', {
					status: 'open',
				})
			},
		}


Event.$on('msg',function(msg){  接收数据,第一个参数是数据的名字,与发送时的名字对应,第二个参数是一个方法,要对数据的操作
    //逻辑区
})

created() {
		uni.$on('play-video', (data) => {
			console.log("我是appId:"+this.$customConfig.appId)
		})
	},

https://www.cnblogs.com/sunsanfeng/p/emit.html 

其它全局变量的引入:https://www.cnblogs.com/yangchin9/p/11002636.html

                                https://www.cnblogs.com/hpx2020/p/10936279.html

一、什么是vuex

        

二、优点

共享数据、易于维护

数据共享、提高开发速度

各个组件之间的数据都是实时同步的

三、什么场景使用

组件之间共享的数据
 

四、引入和使用

        4.1、在根目录下创建 store/index.js 文件

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		
	},
	mutations: {
		
	},
	actions: {
	
	},
	getters: {
		
	}
})

export default store

        4.2、在 main.js 中引入 store/index.js 文件

import Vue from 'vue'
import App from './App'
import store from './store'

Vue.prototype.$store = store

//挂载
const app = new Vue({
	store,
    ...App
})

app.$mount()

        4.3、使用

                4.3.1、所有的共享数据都要统一放到store的state中进行存储

                4.3.2、组件访问state数据

                        4.3.2.1、第一种: this.$store.state.全局变量名称 

                        4.3.2.2、第二种: 从vuex中按需导入mapState函数      

//引入mapGetters
import { mapGetters } from 'vuex'
//按需导入参数
computed: {
    ...mapGetters([
      'count',//需要的参数
    ]),

  },

                4.3.3、赋值(mutations): 用于变更Store中的数据(只有mutations才有变更Store数据的权限)。

                        4.3.3.1、第一种:调用mutations中的方法: this.$store.commit('add', 3);//add为mutations中的一个方法;3为传值

                        4.3.3.2、第二种:

import{ mapMutations } from 'vuex'
	export default {
		computed:{
			...mapMutations([
				'addN'
			]),
		},
        methods:{
            test(){
                this.addN(3) // addN为请求方法、3为请求参数
                
            }
        },
    }

                        4.3.3.3、 mutations 中不允许写异步代码 不如定时器

                                                如果非要使用异步操作可以选择使用actions 

                4.3.4、异步操作(actions):

actions: {
    addAsync(context){
        stTimeout(() =>{
            context.commit('add')
        }, 1000)

    }
}

                        4.3.4.1、第一种: this.$store.dispatch('addAsync')

                        4.3.4.2、第二种: 

                

import{ mapActions } from 'vuex'
	export default {
		computed:{
			...mapActions([
				'addN'
			]),
		},
        methods:{
            test(){
                this.addN(3) // addN为请求方法、3为请求参数
                
            }
        },
    }

                4.3.5、计算属性(getters):

                        4.3.5.1、第一种: this.$store.getters.方法名

                        4.3.5.2、第二种: import { mapGetters } from 'vuex'

     4.4、取值(mapState):

	import{ mapGetters, mapMutations, mapState } from 'vuex'
	export default {
		computed:{
			...mapState([
				'userInfo',
			]),
		},
    }

                 userInfo.avatarUrl

                 

五、

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Vue.js 中,可以使用 Vuex 来进行兄弟组件之间的值传递。Vuex 是 Vue.js 的官方状态管理库,它提供了一个集中式的存储仓库,使得不同组件之间可以共享数据。 要在兄弟组件之间传递值,首先需要在 Vuex 的存储仓库中定义一个状态。你可以在存储仓库中定义一个全局的状态对象,并在其中保存需要共享的值。然后,在需要访问该值的兄弟组件中,可以使用 Vuex 提供的 getter 方法来获取该值。 下面是一个简单的示例: ```javascript // 在 Vuex 的存储仓库中定义状态 const store = new Vuex.Store({ state: { value: null }, mutations: { setValue(state, payload) { state.value = payload; } }, getters: { getValue(state) { return state.value; } } }); // 组件A 设置值 this.$store.commit('setValue', 'Hello from Component A'); // 组件B 获取值 const value = this.$store.getters.getValue; console.log(value); // 输出:Hello from Component A ``` 在组件A中,使用 `this.$store.commit` 方法提交一个名为 `setValue` 的 mutation,并传递需要设置的值。在组件B中,使用 `this.$store.getters` 方法获取名为 `getValue` 的 getter,从而获取值。 通过这种方式,兄弟组件之间就可以进行值的传递了。需要注意的是,组件A和组件B都需要在其 `data` 中引入 Vuex 的存储仓库,以便能够访问共享的值。 希望这个例子能帮助你理解如何在兄弟组件之间使用 Vuex 进行值传递。如果还有其他问题,请随时提问!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值