Vue(四)——全局事件总线, 消息订阅与发布 ,nextTick_vue4 nexttick(1)

	- [2.6. nextTick](#26_nextTick_228)
	- [2.7. Vue封装的过度与动画](#27_Vue_244)

2.4. 全局事件总线

一种组件间通信的方式,适用于任意组件间通信。

  • 安装全局事件总线:
new Vue({
	......
	beforeCreate() {
		Vue.prototype.$bus = this //安装全局事件总线,$bus就是当前应用的vm
	},
    ......
}) 

  • 使用事件总线:
  • 接收数据:A组件想接收数据,则在A组件中给$bus绑定自定义事件,事件的回调留在A组件自身。
methods(){
  demo(data){......}
}
......
mounted() {
  this.$bus.$on('xxxx',this.demo)
}

  • 提供数据:this.$bus.$emit('xxxx',数据)
  • 最好在beforeDestroy钩子中,用$off去解绑当前组件所用到的事件。

School.vue

<template>
	<div class="school">
		<h2>学校名称:{{name}}</h2>
		<h2>学校地址:{{address}}</h2>
	</div>
</template>

<script>
 export default {
 name:'School',
 data() {
 return {
 name:'尚硅谷',
 address:'北京',
 }
 },
 methods: {
 demo(data) {
 console.log('我是School组件,收到了数据',data)
 }
 }
 mounted() {
 // console.log('School',this)
 this.$bus.$on('hello',this.demo)
 },
 beforeDestroy() {
 this.$bus.$off('hello')
 },
 }
</script>

<style scoped>
 .school{
 background-color: skyblue;
 padding: 5px;
 }
</style>

Student.vue

<template>
	<div class="student">
		<h2>学生姓名:{{name}}</h2>
		<h2>学生性别:{{sex}}</h2>
		<button @click="sendStudentName">把学生名给School组件</button>
	</div>
</template>

<script>
 export default {
 name:'Student',
 data() {
 return {
 name:'张三',
 sex:'男',
 }
 },
 mounted() {
 // console.log('Student',this.x)
 },
 methods: {
 sendStudentName(){
 this.$bus.$emit('hello',this.name)
 }
 },
 }
</script>

<style lang="less" scoped>
 .student{
 background-color: pink;
 padding: 5px;
 margin-top: 30px;
 }
</style>


在这里插入图片描述

在这里插入图片描述

2.5. 消息订阅与发布

一种组件间通信的方式,适用于任意组件间通信。

  • 安装pubsub:npm i pubsub-js
  • 引入: import pubsub from 'pubsub-js'
  • 接收数据:A组件想接收数据,则在A组件中订阅消息,订阅的回调留在A组件自身。
methods:{
  demo(data){......}
}
......
mounted() {
  this.pid = pubsub.subscribe('xxx',this.demo) //订阅消息
}

  • 提供数据:pubsub.publish('xxx',数据)
  • 最好在beforeDestroy钩子中,用PubSub.unsubscribe(pid)去取消订阅。

School.vue

<template>
	<div class="school">
		<h2>学校名称:{{name}}</h2>
		<h2>学校地址:{{address}}</h2>
	</div>
</template>

<script>
 import pubsub from 'pubsub-js'
 export default {
 name:'School',
 data() {
 return {
 name:'尚硅谷',
 address:'北京',
 }
 },
 mounted() {
 // console.log('School',this)
 /\* this.$bus.$on('hello',(data)=>{
 console.log('我是School组件,收到了数据',data)
 }) \*/
 this.pubId = pubsub.subscribe('hello',(msgName,data)=>{
 console.log(this)
 // console.log('有人发布了hello消息,hello消息的回调执行了',msgName,data)
 })
 },
 beforeDestroy() {
 // this.$bus.$off('hello')
 pubsub.unsubscribe(this.pubId)
 },
 }
</script>

<style scoped>
 .school{
 background-color: skyblue;
 padding: 5px;
 }
</style>

2.6. nextTick
  1. this.$nextTick(回调函数)
  2. 在下一次 DOM 更新结束后执行其指定的回调。
  3. 当改变数据后,要基于更新后的新DOM进行某些操作时,要在nextTick所指定的回调函数中执行。
this.$nextTick(function(){
	this.$refs.inputTitle.focus()
}

2.7. Vue封装的过度与动画

最后

好了,这就是整理的前端从入门到放弃的学习笔记,还有很多没有整理到,我也算是边学边去整理,后续还会慢慢完善,这些相信够你学一阵子了。

做程序员,做前端工程师,真的是一个学习就会有回报的职业,不看出身高低,不看学历强弱,只要你的技术达到应有的水准,就能够得到对应的回报。

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

学习从来没有一蹴而就,都是持之以恒的,正所谓活到老学到老,真正懂得学习的人,才不会被这个时代的洪流所淘汰。

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值