子组件中设置默认属性

<template>
<div class="child-page">
<h1>我是子组件</h1>
<h3>{{ total }}</h3>
<h3>{{ userInfo }} </h3>
</div>
</template> <script setup>
// 在<script setup>defineProps其实可以不用显示导入,因为编译器会自动处理
import {defineProps} from 'vue'
defineProps({
total:{
type:Number,
default:10
},
userInfo:{
type:Object,
required: true,
default:()=> {
return {
name: '罗峰[金角巨兽]',
age: 100
}
}
}
})

为啥解构失去响应式

解构会让基本数据类型失去响应式。引用数据类型则不会失去响应式。

为啥基本数据类型解构就会失去响应式呢?

回答:因为Vue3使用了Proxy作为响应式的底层实现,而基本数据类型不是可观察的,无法被Proxy拦截。

验证解构失去响应式

// 父组件
<template>
<div class="art-page">
<button @click="changeHandler">改变</button>
<child :total="total" :userInfo="userInfo"></child>
</div>
</template> <script setup>
import child from '@/components/child.vue'
import { ref,reactive } from 'vue';
let total=ref(100)
let userInfo =reactive({name: '张三', age:30})
const changeHandler = ()=>{
total.value += 1000
userInfo.age += 1
}
</script>
// 子组件
<template>
<div class="child-page">
<h1>我是子组件</h1>
<h3>{{ total }}</h3>
<h3>{{ userInfo }} </h3>
<button @click="getValueHandler">获取值</button>
</div>
</template> <script setup>
import {defineProps, toRefs} from 'vue'
let props = defineProps({
total:{
type:Number,
default:10
},
userInfo:{
type:Object,
required: true,
default:()=> {
return {
name: '罗峰[金角巨兽]',
age: 100
}
}
}
})
// 基本数据类型解构失去响应式
const { total, userInfo} =props
const getValueHandler = ()=>{
console.log('total', total)
console.log('userInfo',userInfo)
}
</script>

vue3.5之前使用toRefs或toRef解构不会失去响应式

// 子组件
<template>
<div class="child-page">
<h1>我是子组件</h1>
<h3>{{ total }}--{{ totalValue }}</h3>
<h3>{{ userInfo }} --{{ userInfoObj }}</h3>
<button @click="getValueHandler">获取值</button>
</div>
</template> <script setup>
import {defineProps, toRefs, toRef} from 'vue'
let props = defineProps({
...代码不变,省略
})
// 普通结构失去响应式
const { total, userInfo} =toRefs(props) const totalValue = toRef(props, 'total');
const userInfoObj = toRef(props, 'userInfo') const getValueHandler = ()=>{
console.log('total', total)
console.log('userInfo',userInfo)
}
</script>

vue3.5直接解构不会失去响应式(不需要使用toRefs或者toRef)

<template>
<div class="child-page">
<h1>我是子组件</h1>
<h3>{{ total }}</h3>
<h3>{{ userInfo }} </h3>
<button @click="getValueHandler">获取值</button>
</div>
</template>
<script setup>
import {defineProps } from 'vue'
// vue3.5之后直接解构不会失去响应式
const { total, userInfo} = defineProps({
total:{
type:Number,
default:10
},
userInfo:{
type:Object,
required: true,
default:()=> {
return {
name: '罗峰[金角巨兽]',
age: 100
}
}
}
})
const getValueHandler = ()=>{
console.log('total', total)
console.log('userInfo',userInfo)
}
</script>

vue3.5 解构的时候直接设置默认值

我们刚刚是这样写默认值的,是不是感觉有点麻烦。

const { total, userInfo} = defineProps({
total:{
type:Number,
default:10
},
userInfo:{
type:Object,
required: true,
default:()=> {
return {
name: '罗峰[金角巨兽]',
age: 100
}
}
}
})

vue3.5vue3.5 解构的时候直接设置默认值,与es6的函数设置默认值一样了

<template>
<div class="child-page">
<h1>我是子组件</h1>
<h3>{{ total }}</h3>
<h3>{{ userInfo }} </h3>
<button @click="getValueHandler">获取值</button>
</div>
</template> <script setup>
import {defineProps, toRefs, toRef} from 'vue'
// vue3.5 解构的时候直接设置默认值
const { total=10, userInfo= {name: '罗峰[金角巨兽]', age: 100} } = defineProps({
total:{
type:Number,
// default:10
},
userInfo:{
type:Object,
required: true,
// default:()=> {
// return {
// name: '罗峰[金角巨兽]',
// age: 100
// }
// }
}
})
const getValueHandler = ()=>{
console.log('total', total)
console.log('userInfo',userInfo)
}
</script>

解构后如何监听

<template>
<div class="child-page">
<h1>我是子组件</h1>
<h3>{{ total }}</h3>
<h3>{{ userInfo }} </h3>
</div>
</template> <script setup>
import {defineProps, toRefs, toRef, watch} from 'vue'
const { total=10, userInfo= {name: '罗峰[金角巨兽]', age: 100}} = defineProps({
total:{
type:Number,
},
userInfo:{
type:Object,
required: true,
}
})
// 需要监听解构后的属性,我们要这样写,把它包装在getter中
watch(()=>total, (newValue, oldValue)=>{
console.log('total', newValue)
})
</script>

监听解构后的属性,如果这样写会报错

watch(total, (newValue, oldValue)=>{
console.log('total', newValue)
})

项目会提示:total" is a destructured prop and should not be passed directly to watch(). Pass a getter () => total instead.

大概意思是:total”是一个解构的道具,不应该直接传递给watch()。请传递一个getter()=>total。

vue3.5新增 useTemplateRef

useTemplateRef函数的用法很简单:

只接收一个参数key,这个字符串表示你要获取哪一个节点, 返回值是一个ref变量。

为啥会有这个方法?

我猜想的是通过原来通过ref获取DOM节点会让人分不清楚是变量还是DOM节点。

useTemplateRef 获取DOM节点

<template>
<div class="art-page">
// 我要获取这个节点
<div ref="divNode">我是div</div>
<button @click="getNodeHandler">获取div节点</button>
</div>
</template>
<script lang="ts" setup>
import { useTemplateRef } from 'vue'
// useTemplateRef接受的是一个字符串,这个字符串表示你要获取哪一个节点
const divNode = useTemplateRef<HTMLElement>("divNode");
const getNodeHandler=()=>{
if(divNode.value){
divNode.value.innerText = '通过dom来赋值';
}
// 等价与 divNode.value && (divNode.value.innerText = '通过dom来赋值');
}
</script>

hooks中使用 useTemplateRef

// hooks文件
import { useTemplateRef } from 'vue'
export default function useRef(eleKey:string, contValue:string){
const elementNode = useTemplateRef<HTMLElement>(eleKey);
function setNodeElement(){
console.log(elementNode.value);
if(elementNode.value){
elementNode.value.innerText = contValue;
}
}
return { setNodeElement}
}
// 使用的文件
<template>
<div class="art-page">
<div ref="divNode">我是div</div>
<button @click="getNodeHandler">获取div节点</button>
</div>
</template>
<script lang="ts" setup>
import useRef from '@/hooks/useNode'
const { setNodeElement } = useRef('divNode', '哈哈-这个是hooks');
const getNodeHandler = ()=>{
setNodeElement()
}
</script>

Vue 3.5新增useId 函数

useId是Vue3.5中引入的一个函数,用于生成唯一的ID。

它的主要用途是为组件或DOM元素中唯一的标识符,

避免在 SSR(服务器端渲染)或客户端渲染中因ID重复而导致的问题。

唯一性的前提是:必须在同一个createApp中才是唯一的,如果项目中有多个createApp,那么id就重复了。

多个createApp那么id就重复

// src\main.ts 文件
import { createApp, h, onMounted, useId } from "vue";
createApp({
setup(){
onMounted(()=>{
console.log('app1', useId())
})
return ()=> h('div', 'hello world')
}
}).mount('#app') createApp({
setup(){
onMounted(()=>{
console.log('app2', useId())
})
return ()=> h('div', 'hello world')
}
}).mount('#app')

这个时候我们发现:app1和app2的id是重复的。

多个createApp如何解决id重复问题

我们可以加一个前缀就可以把这个问题给解决了

import { createApp, h, onMounted, useId } from "vue";
createApp({
setup(){
onMounted(()=>{
console.log('app1', useId())
})
return ()=> h('div', 'hello world')
}
}).mount('#app')
let app2 = createApp({
setup(){
onMounted(()=>{
console.log('app2', useId())
})
return ()=> h('div', 'hello world')
}
})
// 给app2加上一个前缀
app2.config.idPrefix = 'app2'
app2.mount('#app')

vue内置组件 Teleport

Vue内置 它可以将Teleport组件的内容移动到指定元素下。

在Teleport组件传送时,vue3.4之前要求目标元素在组件挂载时已经存在。

也就是说: 移动到目标元素必须在Teleport组件的前面。

Vue 3.5 引入了 defer 属性,允许传送的内容到后才渲染目标元素。

目标元素必须在Teleport组件的前面才能渲染

<template>
<div class="art-page">
<Teleport to="#target-node">
<h1>我是传送的h1</h1>
<h1> 等会会被传送</h1>
</Teleport > <p>我是分割线==========我是分割线</p>
// 现在目标元素在Teleport组件的后面,这样渲染会失败的
<div id="target-node"></div>
<p>我是分割线==========我是分割线</p>
</div>
</template>

那怎么处理这个问题呢?

有的小伙伴会说:这个简单,我把目标元素放在Teleport组件的前面就行了。

确实:这样是可以的。换一下位置。

在vue3.5的版本中,我们只需要使用defer属性即可。

vue3.5中defer属性的作用

以通过 defer 来延迟 Teleport 的挂载。

它会等到同一更新周期中的所有其他 DOM 内容都渲染完毕后,再挂载到目标容器下。

延迟传送(defer Teleport)

<template>
<div class="art-page">
<Teleport defer to="#target-node">
<h1>我是传送的h1</h1>
<h1> 等会会被传送</h1>
</Teleport > <p>我是分割线==========我是分割线</p>
<div id="target-node"></div>
<p>我是分割线==========我是分割线</p>
</div>
</template>

onWatcherCleanup

该函数将在观察程序失效并即将重新运行时调用。

意思是说:它允许我们在观察的目标发生变化之前执行一些清理工作。

这样我们就可以取消网络请求、移除事件监听器等

onWatcherCleanup的注意点:

1,仅在 Vue 3.5+ 中支持。

2,并且必须在同步执行 watchEffect effect 函数或 watch 回调函数时调用,

你不能在异步函数中的 await 语句之后调用它。

点击按钮3次,就会发送3次请求。

<template>
<div class="art-page">
<button @click="num++">点击按钮</button>
</div>
</template>
<script lang="ts" setup>
import { ref, watch } from 'vue';
let num =ref(0)
watch(num, ()=>{
setTimeout(function(){
console.log( '我会模拟发送请求', num.value)
}, 2000)
})
</script>

10-触发3次.jpg

通过上面的图片,我们发现在1s内点击按钮3次

那么请求就会执行3次,这样并不好。

我们只希望触发最后一次请求。

这个时候我们的主角onWatcherCleanup就闪亮登场了。

vue3.5让它只发送一次请求

<template>
<div class="art-page">
<button @click="num++">点击按钮</button>
</div>
</template> <script lang="ts" setup>
import { onWatcherCleanup, ref, watch } from 'vue'; let num =ref(0)
watch(num, ()=>{
let timer= setTimeout(function(){
console.log( '我会发送请求执行', num.value)
}, 2000)
//
onWatcherCleanup(()=>{
clearTimeout(timer)
})
})

vue3.5之前的处理办法,使用watch的第3个参数来处理

<template>
<div class="art-page">
<button @click="num++">点击按钮</button>
</div>
</template>
<script lang="ts" setup>
import { onWatcherCleanup, ref, watch } from 'vue';
let num =ref(0)
watch(num, (newValue,oldValue, onCleanup)=>{
let timer= setTimeout(function(){
console.log( '我会发送请求执行', num.value)
}, 2000)
//
onCleanup(()=>{
clearTimeout(timer)
})
})
</script>

vue3.5保证你看得明明白白的更多相关文章

  1. Vue3.2中的setup语法糖,保证你看的明明白白!

    vue3.2 到底更新了什么? 根据原文内容的更新的内容主要有以下 5 块: 1.SSR:服务端渲染优化.@vue/server-renderer包加了一个ES模块创建, 与Node.js解耦,使在非 ...

  2. 国庆总结:echarts自定义颜色主题,保证你看的明明白白

    为什么需要使用颜色主题 随着用户审美越来越高,不再是过去那样只注重功能. 所以对界面的颜色样式都具有一定的审美要求 此时颜色是否好看就非常重要了 因为人都是视觉动物 对界面的第一印象肯定都是颜色. 如 ...

  3. vue下一代状态管理Pinia.js 保证你看的明明白白!

    1.pinia的简单介绍 Pinia最初是在2019年11月左右重新设计使用Composition API的 Vue 商店外观的实验. 从那时起,最初的原则相同,但 Pinia 适用于 Vue 2 和 ...

  4. vue混入mixin的使用,保证你看的明明白白!

    场景描述 有些时候,我们发现有些组件部分功能代码是几乎是一样的. 这个时候,我们就可以将相同的逻辑代码抽离出来 此时我们的主角混入mixin就登场了 下面我们有a-test和b-test两个组件,点击 ...

  5. node使用node-xlsx实现excel的下载与导入,保证你看的明明白白

    需求简介 很多时候,我们都会有这样一个业务. 将列表中的数据导出为excel. 这样做的目的是为了方便查看,同时可以保存在本地归档. 还可以将导出的Excel后的数据进行加工. node-xlsx 的 ...

  6. 面试题——20+Vue面试题整理

    0.那你能讲一讲MVVM吗? MVVM是Model-View-ViewModel缩写,也就是把MVC中的Controller演变成ViewModel. Model层代表数据模型,View代表UI组件, ...

  7. ES6 新特性

    ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准.因为当前版本的ES6是在2015年发布的,所以又称ECMAScript 2015. 也就是说,ES6就是ES2015. ...

  8. 最常用的ES6特性(转)

    最常用的ES6特性 let, const, class, extends, super, arrow functions, template string, destructuring, defaul ...

  9. java 大数据处理类 BigDecimal 解析

    这两天,由于我的必修课概率论里经常要用到排列组合的计算,感觉很麻烦,加上现代智能手机的计算器是没有这方面功能的. 所以,就自己动手写了个安卓的 排列组合 计算器,用了一天,发现有很大的问题,阶乘达百亿 ...

  10. 算法:KMP算法

    算法:KMP排序 算法分析 KMP算法是一种快速的模式匹配算法.KMP是三位大师:D.E.Knuth.J.H.Morris和V.R.Pratt同时发现的,所以取首字母组成KMP. 少部分图片来自孤~影 ...

随机推荐

  1. 5.Kubeadm和二进制方式对比

    Kubeadm方式搭建K8S集群 安装虚拟机,在虚拟机安装Linux操作系统[3台虚拟机] 对操作系统初始化操作 所有节点安装Docker.kubeadm.kubelet.kubectl[包含mast ...

  2. python操作sqlite的小例子

    照着菜鸟教程 学习python操作sqlite ubuntu 安装 sudo apte-get install sqlite3 找到了 sqlite3/bionic-updates,bionic-se ...

  3. python模块之sqlite3

    在Python中操作sqlite3 1)基本使用 import sqlite3 conn = sqlite3.connect('example.db') cursor = conn.cursor() ...

  4. Postgresql之基础

    Postgresql:  https://www.postgresql.org/ [安装] 删除已经存在的pg: yum remove -y postgresql* && rm -rf ...

  5. vim之常用插件

    Vundle是vim的一个插件管理器, 同时它本身也是vim的一个插件.插件管理器用于方便.快速的安装.删除.Vim更新插件.vim Vundle插件官方地址:https://github.com/V ...

  6. whisper v3 finetune 中文乱码问题的解决方案

    最近学习了一下whisper的微调,主要是参考了github上的夜雨飘零大神项目.但是在操作中遇到了微调中文的时候出现了乱码的情况.以下是我这边对于微调过程中中文出现乱码情况的解决方案. 出现情况如下 ...

  7. R数据分析:生存数据预测模型的建立和评价(二)timeROC与决策曲线

    上篇文章依照jama surgery的一篇文章给大家写了生存数据预测模型评价的C指数.校准曲线和模型验证结果的做法,其实生存数据预测模型的评价方法还有很多,本期接着往下看. Time-dependen ...

  8. ABS函数:C语言与Excel中的绝对值计算

    ABS函数:C语言与Excel中的绝对值计算 ABS函数在不同的编程和计算环境中有着相似但又有所区别的用途.在本文中,我们将重点探讨ABS函数在C语言中的应用,同时也会结合Excel中的ABS函数进行 ...

  9. 工作中这样用MQ,很香!

    前言 消息队列(MQ)是分布式系统中不可或缺的技术之一. 对很多小伙伴来说,刚接触MQ时,可能觉得它只是个"传话工具",但用着用着,你会发现它简直是系统的"润滑剂&quo ...

  10. 动态 import()

    动态 import() https://v8.dev/features/dynamic-import Dynamic import() 引入了一个新的类似函数的功能,相比静态的 import 提供了新 ...