Vue3技术8之Fragment、Suspense、Vue3中其他的改变

news/2024/10/20 0:37:56/

Vue3技术8

  • Fragment
  • Teleport
    • 弹窗案例
      • 目录结构
      • App.vue
      • Child.vue
      • Son.vue
      • Dialog.vue
    • 总结
  • Suspense
    • 普通写法
      • App.vue
      • Child.vue
    • 使用suspense之后
      • App.vue
      • Child.vue
    • 不再自己调整网络低速
      • Child.vue
      • App.vue
    • 总结
      • 补充setup的一个知识点
      • Suspense总结
  • Vue3中其他的改变
    • 全局API的转移
    • 其他改变

Fragment

  1. 在Vue2中:组件必须有一个根标签
  2. 在Vue3中:组件可以没有根标签,内部会将多个标签包含在一个Fragment虚拟元素中
  3. 好处:减少标签层级,减少内存占用
    在这里插入图片描述

Teleport

弹窗案例

目录结构

在这里插入图片描述

App.vue

<template><div class="app"><h1>我是App组件</h1><Child></Child></div>
</template><script>
import Child from "@/components/Child";export default {name: 'App',components: {Child},
}
</script><style scoped>
.app{background-color: gray;padding: 10px;
}
</style>

Child.vue

<template><div class="child"><h1>我是Child组件</h1><Son></Son></div>
</template><script>
import Son from "@/components/Son";
export default {name: "Child",components: {Son}
}
</script><style scoped>
.child{background-color: orange;padding: 10px;
}
</style>

Son.vue

<template><div class="son"><h1>我是Son组件</h1><Dialog></Dialog></div>
</template><script>
import Dialog from "@/components/Dialog";
export default {name: "Son",components: {Dialog},
}
</script><style scoped>
.son{background-color: pink;padding: 10px;
}
</style>

Dialog.vue

<template><div><button @click="isShow=true">点我弹个窗</button><teleport to="body"><div class="mask"  v-if="isShow"><div class="dialog"><h3>我是一个弹窗</h3><h4>一些内容</h4><h4>一些内容</h4><h4>一些内容</h4><button @click="isShow=false">关闭弹窗</button></div></div></teleport></div>
</template><script>
import {ref} from "vue";export default {name: "Dialog",setup(){let isShow=ref(false)return{isShow}},
}
</script><style scoped>
.mask{position: absolute;top: 0;bottom: 0;left: 0;right: 0;background-color: rgba(0,0,0,0.5);
}
.dialog{width: 300px;height: 300px;background-color: skyblue;text-align: center;position: absolute;left: 50%;top: 50%;transform: translate(-50%,-50%);
}
</style>

请添加图片描述

总结

  1. 什么是Teleport?——Teleport是一种能将我们的组件html结构移动到指定位置的技术
<teleport to="移动位置"><div v-if="isShow" class="mask"><div class="dialog"><h3>我是一个弹窗</h3><button @click="isShow=false">关闭弹窗</button></div></div>
</teleport>

Suspense

普通写法

App组件和Child组件是一起加载出来的

App.vue

<template><div class="app"><h1>我是App组件</h1><Child></Child></div>
</template><script>
import Child from "@/components/Child";  //静态引入export default {name: 'App',components: {Child},
}
</script><style scoped>
.app{background-color: gray;padding: 10px;
}
</style>

Child.vue

<template><div class="child"><h1>我是Child组件</h1></div>
</template><script>
export default {name: "Child",
}
</script><style scoped>
.child{background-color: orange;padding: 10px;
}
</style>

请添加图片描述

使用suspense之后

App.vue

<template><div class="app"><h1>我是App组件</h1><suspense><template v-slot:default><Child></Child></template><template v-slot:fallback><h3>稍等,加载中...</h3></template></suspense></div>
</template><script>
// import Child from "@/components/Child";  //静态引入
import {defineAsyncComponent} from 'vue'
const Child=defineAsyncComponent(()=>import('@/components/Child'))  //异步引入
export default {name: 'App',components: {Child},
}
</script><style scoped>
.app{background-color: gray;padding: 10px;
}
</style>

Child.vue

<template><div class="child"><h1>我是Child组件</h1></div>
</template><script>
export default {name: "Child",
}
</script><style scoped>
.child{background-color: orange;padding: 10px;
}
</style>

请添加图片描述

不再自己调整网络低速

Child.vue

<template><div class="child"><h1>我是Child组件</h1><div>{{sum}}</div></div>
</template><script>
import {ref} from "vue";export default {name: "Child",async setup(){let sum=ref(0)const p=  new Promise((resolve, reject) => {setTimeout(() => {resolve({sum})},3000)})return await p}
}
</script><style scoped>
.child{background-color: orange;padding: 10px;
}
</style>

App.vue

<template><div class="app"><h1>我是App组件</h1><suspense><template v-slot:default><Child></Child></template><template v-slot:fallback><h3>稍等,加载中...</h3></template></suspense></div>
</template><script>
// import Child from "@/components/Child";  //静态引入
import {defineAsyncComponent} from 'vue'
const Child=defineAsyncComponent(()=>import('@/components/Child'))  //异步引入
export default {name: 'App',components: {Child},
}
</script><style scoped>
.app{background-color: gray;padding: 10px;
}
</style>

总结

补充setup的一个知识点

  1. setup不能是一个async函数,因为返回值不再是return的对象,而是promise,模板看不到return对象中的属性。(后期也可以返回一个Promise实例,但需要Suspense和异步组件的配合)

Suspense总结

  1. 等待异步组件时渲染一些额外内容,让应用有更好的用户体验
  2. 使用步骤
    (1)异步引入组件
import {defineAsyncComponent} from 'vue'
const Child=defineAsyncComponent(()=>import('@/components/Child'))  //异步引入

(2)使用Suspense包裹组件,并配置好defaultfallback

<template><div class="app"><h1>我是App组件</h1><suspense><template v-slot:default><Child/></template><template v-slot:fallback><h3>稍等,加载中...</h3></template></suspense></div>
</template>

Vue3中其他的改变

全局API的转移

  1. Vue 2.x 有许多全局 API 和配置。
    例如:注册全局组件、注册全局指令等
//注册全局组件Vue.component('MyButton', {data: () => ({count: 0}),template: '<button @click="count++">Clicked {{ count }} times.</button>'})//注册全局指令Vue.directive('focus', {inserted: el => el.focus()}
  1. Vue3中对这些API做了调整
    将全局的API,即:Vue.xxx调整到应用实例(app)

    2.x 全局 API(Vue3.x 实例 API (app)
    Vue.config.xxxxapp.config.xxxx
    Vue.config.productionTip移除
    Vue.componentapp.component
    Vue.directiveapp.directive
    Vue.mixinapp.mixin
    Vue.useapp.use
    Vue.prototypeapp.config.globalProperties

其他改变

  1. data选项应始终被声明为一个函数。
  2. 过度类名的更改:
  • Vue2.x写法

    .v-enter,
    .v-leave-to {opacity: 0;
    }
    .v-leave,
    .v-enter-to {opacity: 1;
    }
    
  • Vue3.x写法

    .v-enter-from,
    .v-leave-to {opacity: 0;
    }.v-leave-from,
    .v-enter-to {opacity: 1;
    }
    
  1. 移除keyCode作为 v-on 的修饰符,同时也不再支持config.keyCodes
  2. 移除修饰符
  • 父组件中绑定事件

    <my-componentv-on:close="handleComponentEvent"v-on:click="handleNativeClickEvent"
    />
    
  • 子组件中声明自定义事件

    <script>export default {emits: ['close']}
    </script>
    
  1. 移除过滤器(filter)

过滤器虽然这看起来很方便,但它需要一个自定义语法,打破大括号内表达式是 “只是 JavaScript” 的假设,这不仅有学习成本,而且有实现成本!建议用方法调用或计算属性去替换过滤器。


http://www.ppmy.cn/news/53896.html

相关文章

RRC reconfiguration failure场景介绍

本文介绍UE在ENDC连接状态下,从网络接收到RRC重新配置消息以重新配置SCG(辅助小区组)失败场景。 RRC重新配置消息的IE(信息元素)检查失败,因为网络在发送RRC重新配置消息时没有包含“reestablishRLC”这个IE。如果UE接受了这种没有包含“reestablishRLC”的配置,会导致…

Linux下一切皆文件与指令的本质(可执行程序),which指令等

Linux下一切皆文件 在Linux下的话&#xff0c;一切皆文件。主要是看待诸如软硬件设备与磁盘文件的看法&#xff1a;一切皆文件&#xff0c;比如说显示器它也是文件&#xff0c;键盘也是文件&#xff0c;普通文件肯定是文件。首先就是显示器这个东西&#xff0c;它其实就是可以打…

标准ACL配置

标准ACL配置 【实验目的】 掌握标准ACL的配置。 验证配置。 【实验拓扑】 实验拓扑如图1所示。 图1 实验拓扑 设备参数如表所示。 表1 设备参数表 设备 接口 IP地址 子网掩码 默认网关 R1 S0/3/0 192.168.1.1 255.255.255.252 N/A Gi0/0/0 192.168.2.1 255.…

【python中的魔法方法有哪些?】

__init__(self, ...): 类的构造函数&#xff0c;用于创建一个类的实例并初始化它的属性。__str__(self): 返回对象的字符串表示形式&#xff0c;可以用于打印对象或者转化成字符串。__repr__(self): 返回对象的字符串表示形式&#xff0c;通常是用于开发者调试和查看对象信息。…

Qt中JSON字符串的使用

Qt中JSON字符串的使用 JSON是一种对源自Javascript的对象数据进行编码的格式&#xff0c;但现在广泛用作互联网上的数据交换格式。有一种应用场景&#xff1a;存储变化的坐标对。存储JSON数组字符串即可。 从 Qt 5.0 就开始提供处理 Json 数据的操作支持&#xff0c;Qt中的JS…

数据结构,二叉树

1.二叉树 度&#xff1a;每一个节点的子节点数量 二叉树中&#xff0c;任意节点的度<2 根节点&#xff1a;最顶层的节点 数高&#xff1a;数的总层数 左子节点&#xff1a;左下方的节点 根节点的左子树&#xff1a;根节点的左边部分 2.二叉查找树 特点&#xff1a; …

设定 SLO 有哪些原则

针对核心和非核心应用&#xff0c;以及强弱依赖关系&#xff0c;我们在设定 SLO 时的要求也是不同的&#xff0c;具体来说&#xff0c;可以采取下面 4 个原则。 第一&#xff0c;核心应用的 SLO 要更严格&#xff0c;非核心应用可以放宽。 这么做&#xff0c;就是为了确保 SRE…

stp 相关配置

display stp [Huawei]display stp interface GigabitEthernet 0/0/1 display stp brief []stp mode {stp|rstp|mstp} 默认mstp undo stp enable 关闭交换机或某个接口stp协议 stp root primary 指定根桥&#xff0c;优先级0且不能更改 stp root secondary 指定备…