一、消息订阅与发布(pubsub:一个第三方的js库)
1. 一种组件间通信的方式,适用于任意组件间通信。
2. 使用步骤:
1. 安装pubsub:npm i pubsub-js
2. 引入: import pubsub from 'pubsub-js'
3. 接收数据:A组件想接收数据,则在A组件中订阅消息,订阅的回调留在A组件自身。
methods(){
demo(data){......}
}
......
mounted() {
this.pid = pubsub.subscribe('xxx',this.demo) //订阅消息
}
4. 提供数据:pubsub.publish('xxx',数据)
5. 最好在beforeDestroy钩子中,用 PubSub.unsubscribe(pid) 去取消订阅。
// 订阅消息
<script>
import pubsub from 'pubsub-js'
export default {
mounted() {
// 订阅消息,消息名是'hello',第二个参数是回调函数
this.pubId = pubsub.subscribe('hello',(msgName,data)=>{
console.log(this)
})
},
beforeDestroy() {
pubsub.unsubscribe(this.pubId)
}
}
</script>
// 发布消息
<script>
import pubsub from 'pubsub-js'
export default {
methods: {
sendStudentName(){
pubsub.publish('hello',666)
}
},
}
</script>
二、$nextTick(vc身上)
1. 语法:this.$nextTick(回调函数)
2. 作用:在下一次 DOM 更新结束后执行其指定的回调。
3. 什么时候用:当改变数据后,要基于更新后的新DOM进行某些操作时,要在nextTick所指定的回调函数中执行。
1、nextTick方法是一种异步更新DOM的机制,它的原理是利用JS的事件循环机制以及浏览器的渲染流程来实现
延迟执行DOM的操作,它主要是为了解决Vue的异步更新导致的DOM更新后的操作问题。
当在代码中使用nextTick方法时,框架会将待更新的DOM操作推入一个队列中,然后在当前JS任务执行完成之后,利用宏任务或微任务的机制进行执行,以确保代码逻辑执行完成后再去操作DOM。nextTick 方法会在下一次 DOM 更新循环结束后执行一个回调函数。这样我们就能确保在操作 DOM 元素之前,DOM 已经更新完成。
// 使用场景
1、操作更新后的DOM:
当需要对更新后的 DOM 进行操作时,在使用 Vue.js 或其他类似框架的情况下,可以将 DOM 操作代码包裹
在 `nextTick` 的回调函数中。这样可以确保 DOM 更新已经完成,并且在下一个 「DOM 更新循环」中执行
操作,避免出现操作未生效的问题。
2、异步更新后的操作:
当需要在 DOM 更新后执行一些异步操作时,如在表单数据更新后提交表单、在列表数据更新后进行滚动定位
等,可以在 `nextTick` 回调函数中触发相应的异步操作。这样可以保证在下一个事件循环周期中执行操作,
以确保更新已经完成。
注意事项:(1)nextTick 方法是一个实例方法,在 Vue 组件中可以直接使用,但在其他地方需要通过 Vue 实例来调用,例如:this.$nextTick()。(2) nextTick 方法是异步的,回调函数会在下一次 DOM 更新循环结束后执行,因此并不是立即执行的。(3) nextTick 方法支持使用 Promise 或返回 Promise 的函数来进行链式调用。
三、Vue封装的过度与动画
1、动画效果
// 动画效果(html实现)
@keyframes donghua {
from{
transform: translateX(-100%);
}
to{
transform: translateX(0px);
}
}
<template>
<div>
<button @click="isShow = !isShow">显示/隐藏</button>
<!-- vue的要求是:想要哪个标签有动画效果,那么就用<transition>标签包裹起来 -->
<!-- <transition>标签如果不起class名,那么.v-enter-active就可以 -->
<!-- appear属性:一出现就会运行 -->
<transition name="hello" appear>
<h1 v-show="isShow">你好啊!</h1>
</transition>
</div>
</template>
<style scoped>
.hello-enter-active{
animation: donghua 0.5s linear;
}
.hello-leave-active{
animation: donghua 0.5s linear reverse;
}
@keyframes donghua {
from{
transform: translateX(-100%);
}
to{
transform: translateX(0px);
}
}
</style>
2、过度效果(多个元素一起过度效果)
<template>
<div>
<button @click="isShow = !isShow">显示/隐藏</button>
<transition-group name="hello" appear>
<!-- <transition-group>标签,多个 -->
<!-- key:唯一标识 -->
<h1 v-show="!isShow" key="1">你好啊!</h1>
<h1 v-show="isShow" key="2">北京</h1>
</transition-group>
</div>
</template>
<script>
export default {
name:'Test',
data() {
return {
isShow:true
}
},
}
</script>
<style scoped>
h1{
background-color: orange;
}
/* 进入的起点、离开的终点 */
.hello-enter,.hello-leave-to{
transform: tran slateX(-100%);
}
// 进入的过程中,离开的过程中
.hello-enter-active,.hello-leave-active{
transition: 0.5s linear;
}
/* 进入的终点、离开的起点 */
.hello-enter-to,.hello-leave{
transform: translateX(0);
}
</style>
3、集成第三方动画(animate.css动画库) 安装、引入 第三方js库
<template>
<div>
<button @click="isShow = !isShow">显示/隐藏</button>
<transition-group
appear
name="animate__animated animate__bounce"
<!-- 进入的动画 -->
enter-active-class="animate__swing"
<!-- 离开的动画 -->
leave-active-class="animate__backOutUp"
>
<h1 v-show="!isShow" key="1">你好啊!</h1>
<h1 v-show="isShow" key="2">北京!</h1>
</transition-group>
</div>
</template>
<script>
import 'animate.css'
export default {
name:'Test',
data() {
return {
isShow:true
}
},
}
</script>
<style scoped>
h1{
background-color: orange;
}
</style>
// Vue封装的过度与动画
1. 作用:在插入、更新或移除 DOM元素时,在合适的时候给元素添加样式类名。
2. 图示
3. 写法:
1. 准备好样式:
元素进入的样式:
1. v-enter:进入的起点
2. v-enter-active:进入过程中
3. v-enter-to:进入的终点
元素离开的样式:
1. v-leave:离开的起点
2. v-leave-active:离开过程中
3. v-leave-to:离开的终点
2. 使用 <transition> 包裹要过度的元素,并配置name属性:
<transition name="hello">
<h1 v-show="isShow">你好啊!</h1>
</transition>
3. 备注:若有多个元素需要过度,则需要使用:<transition-group>,且每个元素都要指定 key 值。