五、Vue3基础之五


一、vue3 slot

插槽是子组件中提供给父组件使用的一个占位符,父组件可以在这个占位符中填充任何模板代码,如html、组件等。
子组件提供坑,坑里填什么由父组件决定。

子组件挖坑:

<template>

    <div>
        <div class="div1">
            <!-- 匿名插槽 挖坑 -->
            <slot></slot>
        </div>
        <div class="div2">
            <!-- 挖坑-->
            <slot name="div2"></slot>
        </div>
        <div class="div3">

        </div>
    </div>
    
</template>

<script setup lang='ts'>
import { ref, reactive } from 'vue'

</script>

<style lang='scss' scoped>
.div1{
    width: 200px;
    height: 200px;
    background-color: yellow;
}
.div2{
    width: 200px;
    height: 200px;
    background-color: blueviolet;
}
.div3{
    width: 200px;
    height: 200px;
    background-color: pink
}

</style>

父组件填坑

<template>
    <div>
        右侧内容区域
        <A>
            <!-- 填坑 -->
            <template v-slot>
                <h2>匿名插槽</h2>
            </template>
            <template v-slot:div2>
                <h2>具名插槽</h2>
            </template>
        </A>
    </div>
</template>

<script setup lang='ts'>
import { ref, reactive, markRaw } from 'vue'
import A from './A.vue'
import B from './B.vue'
import C from './C.vue'
</script>
<style lang='scss' scoped>
</style>

子组件通过插槽向父组件传数据
子组件

<template>

    <div>
        <div class="div1">
            <!-- 匿名插槽 -->
            <slot></slot>
        </div>
        <div class="div2">
            <div v-for="(item, index) in data">
                <!-- 通过这个坑向父组件传数据过去了 -->
                <slot name="div2" :index="index" :data="item"></slot>
            </div>
            
        </div>
        <div class="div3">

        </div>
    </div>

</template>

<script setup lang='ts'>
import { ref, reactive } from 'vue'
type Person = {
    name: string, 
    age: number
}
const data = reactive<Person[]>([
    {
        name: '张三',
        age: 18
    },
    {
        name: '李',
        age: 19
    },
    {
        name: '王五',
        age: 20
    },
    {
        name: '赵六',
        age: 21
    },
])

</script>

<style lang='scss' scoped>
.div1{
    width: 200px;
    height: 200px;
    background-color: yellow;
}
.div2{
    width: 200px;
    height: 200px;
    background-color: blueviolet;
}
.div3{
    width: 200px;
    height: 200px;
    background-color: pink
}

</style>

父组件

<template>
    <div>
        右侧内容区域
        <A>
            <template #default>
                <h2>匿名插槽</h2>
            </template>
            <template v-slot:div2="{index, data}">
                <h2>具名插槽--{{index}}--{{data.name}}--{{data.age}}</h2>
            </template>
        </A>
    </div>
</template>

<script setup lang='ts'>
import { ref, reactive, markRaw } from 'vue'
import A from './A.vue'
import B from './B.vue'
import C from './C.vue'


</script>

<style lang='scss' scoped>

</style>

动态插槽

<template>
    <div>
        右侧内容区域
        <A>
            <!-- 这个name我可以动态给它赋值就达到了动态插槽 -->
            <template #[name]>
                <h2>匿名插槽</h2>
            </template>

        </A>
    </div>
</template>

<script setup lang='ts'>
import { ref, reactive, markRaw } from 'vue'
import A from './A.vue'

let name = ref('div2')

</script>

<style lang='scss' scoped>

</style>

二、异步组件

我们打包发布的时候会打成一个js文件,如果定义成异步组件,就会单独打包成一个js文件,用到的时候再加载进来。
在public文件下建一 个data.json文件,用来模拟数据

[
    {
        "name": "张三"
    },
    {
        "name": "李四"
    },
    {
        "name": "王五"
    },
    {
        "name": "赵六"
    }
]

在Content下建一个server.ts用来模拟axios,异步获取数据

type NameList = {
    name: string
}

export const axios = (url:string):Promise<NameList[]> => {
    return new Promise((resolve) => {
        let xhr: XMLHttpRequest = new XMLHttpRequest()
        xhr.open('get', url)
        xhr.onreadystatechange = () => {
            if(xhr.readyState ===4 && xhr.status == 200){
                setTimeout(()=>{
                    resolve(JSON.parse(xhr.responseText))
                }, 2000)
            }
        }
    })
}

父组件

<template>
    <div>
        右侧内容区域
        <Suspense>
            <!-- 异步组件获取到了数据的显示 -->
            <template #default>
                <C></C>
            </template>
            <!-- 异步组件未获取到的数据显示-->
            <template #fallback>
                 loading......
            </template>
        </Suspense>
    </div>
</template>

<script setup lang='ts'>
import { defineAsyncComponent } from 'vue'
// 用defineAsyncComponent来加载异步组件
const C = defineAsyncComponent(()=>import('./C.vue'))

</script>

<style lang='scss' scoped>

</style>

子组件

<template>

    <div>
        CCCCCCCCCCCCCCCCCCCC
        <ul v-for="item,index in list" :key="index">
            <li>{{item.name}}</li>
        </ul>
    </div>

</template>

<script setup lang='ts'>
import { ref, reactive } from 'vue'
import { axios } from './server'

// 在public下的文件为什么这里请求是./data.json呢?
// await自动获取到.then的数据
const list = await axios('./data.json')
console.log(list)

</script>

<style lang='scss' scoped>

</style>

三、vue3 Teleport传送组件

Vue3.0新特性之一,能够将我们的模板渲染到指定DOM节点,不受父组style,v-show等属性影响,但data,prop数据依旧能够共用的技术。
Teleport节点搭载在其他指定的DOM节点下,完全不受父级style样式影响。

<Teleport to="body">
     <!-- 将A组件放进body中,独立style等-->
    <A></A>
</Teleport>

四、vue3 keep-alive缓存组件

有时候我们希望组件被重新渲染影响使用体验,或者处于性能考虑,避免多次重复渲染降低性能,而是希望组件可以缓存下来维持当前的状态,这时候就要用到keep-alive组件。

<keep-alive>
     <!-- 
     被keep-alive包裹的组件会被缓存,如A组件中有一个表单,
     你输入了一些东西,但用户有其他事切换到了其他组件中,返
     回这个A组件时输入的东西还在,可以提高用户体验
     <keep-alive :include="['A']"> 只包含A组件
     :exclude 不包含某某组件
     -->
    <A v-if="flag"><A>
    <B v-else><B>
</keep-alive>

使用了keep-alive包裹的组件新增了两个生命周期

onActivated(()=>{
    console.log('keep-alive初始化')
})
onDeactivated(()=>{
    console.log('keep-alive卸载')
})

五、vue3 transition动画组件

vue提供了transtion的封装组件,在下列情况中,可以给任何元素和组件添加进入、离开过渡

  • 条件渲染(使用v-if)
  • 条件展示(使用v-show)
  • 动态组件
  • 组件根节点

案例:transition动画组件使用

<template>
    <div>
        <button @click="flag=!flag">switch</button>
        <transition name="fade">
            <div v-if="flag" class="box"></div>
        </transition>
    </div>
</template>

<script setup lang='ts'>
import { ref } from 'vue'

const flag = ref<boolean>(true)

</script>

<style lang='scss' scoped>
.box{
    width: 200px;
    height: 200px;
    background-color: palevioletred;
}
// 进入动画之前,具体的属性可翻看vue官方文档
.fade-enter-from{
    width: 0;
    height: 0;
}
.fade-enter-active{
    transition: all 1.5s ease;
}
.fade-enter-to{
    width: 200px;
    height: 200px;
}
</style>

vue3的动画组件有很多特征,可以自行看一下。

六、Provide、Inject

在这里插入图片描述
在这里插入图片描述
App.vue

<template>
    <h1>App.vue根组件</h1>
    <label>
        <input v-model="colorVal" value="red" name="color" type="radio">
        红色
    </label>
    <label>
        <input v-model="colorVal" value="pink" name="color" type="radio">
        粉色
    </label>
    <label>
        <input v-model="colorVal" value="yellow" name="color" type="radio">
        黄色
    </label>
    
    <div class="box"></div>
    <provideA></provideA>

</template>

<script setup lang="ts">
import {ref, provide} from 'vue'
import provideA from './components/provideA.vue'
const colorVal = ref<string>('red')
// 将数据注入进去,子组件中就可以使用inject读取这个数据了
provide('color', colorVal)
</script>

<style lang="scss" scoped>
.box{
    width: 50px;
    height: 50px;
    border: 1px solid red;
    background-color: v-bind(colorVal);
}
</style>

provideA.vue

<template>

    <h2>provideA</h2>
    <div class="box"></div>
    <provideB></provideB>
</template>

<script setup lang='ts'>
import { ref, reactive, inject } from 'vue'
import { Ref } from 'vue'
import provideB from './provideB.vue'
const color = inject<Ref<string>>('color')


</script>

<style lang='scss' scoped>
.box{
    width: 50px;
    height: 50px;
    border: 1px solid red;
    // vue3中css可以使用v-bind绑定setup中的变量
    background-color: v-bind(color);
}
</style>

provideB.vue

<template>

    <h2>provideB</h2>
    <div class="box">

    </div>

</template>

<script setup lang='ts'>
import { ref, reactive, inject } from 'vue'
import { Ref } from 'vue'
const color = inject<Ref<string>>('color')

</script>

<style lang='scss' scoped>
.box{
    width: 50px;
    height: 50px;
    border: 1px solid red;
    background-color: v-bind(color);
}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

永恒的宁静

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值