当前位置: 首页 > news >正文

记录使用 Vue3 过程中的一些技术点

1、自定义组件,并使用 v-model 进行数据双向绑定。

简述: 自定义组件使用 v-model 进行传参时,遵循 Vue 3 的 v-model 机制。在 Vue 3 中,v-model 默认使用了 modelValue 作为 prop 名称,以及 update:modelValue 作为事件名称。

例子:
首先,我们创建一个自定义组件 MyInput.vue,该组件使用 <script setup> 语法,并允许通过 v-model 绑定值:

<!-- MyInput.vue -->  
<template>  <input :value="modelValue" @input="updateValue" />  
</template>  <script setup>  
import { defineProps, defineEmits, ref } from 'vue';  const props = defineProps({  modelValue: String  
});  const emit = defineEmits(['update:modelValue']);  const updateValue = (event) => {  emit('update:modelValue', event.target.value);  
};  
</script>

在这个例子中,使用了 defineProps 来定义 modelValue prop,它对应于 v-model 绑定的值。同时,我们使用 defineEmits 来声明 update:modelValue 事件,该事件将在输入框的值变化时被触发。

接下来,在父组件中使用这个自定义组件,并通过 v-model 绑定一个数据属性:

<!-- ParentComponent.vue -->  
<template>  <div>  <p>Value in Parent: {{ inputValue }}</p>  <MyInput v-model="inputValue" />  </div>  
</template>  <script setup>  
import { ref } from 'vue';  
import MyInput from './MyInput.vue';  const inputValue = ref('');  
</script>

在父组件中,导入了自定义的 MyInput 组件,并使用 v-modelinputValue 绑定到该组件的 modelValue prop 上。当 MyInput 组件中的输入框值变化时,它会触发 update:modelValue 事件,进而更新父组件中的 inputValue

注意,在 <script setup> 中,不需要显式地返回任何东西给模板,因为所有的响应式状态(通过 refreactive 创建)和函数都会自动暴露给模板。这使得代码更加简洁和直观。

2、异步加载动态组件
  ├─ src              │  ├─ components              │  │  ├─ ChartA.vue         │  │  ├─ ChartB.vue        │  │  └─ ChartC.vue       │  └─ test-async.vue └─ package.json                       
异步加载组件

方式一:


<template><AsyncOne />
</template><script setup>
import { defineAsyncComponent } from "vue";const AsyncOne = defineAsyncComponent(() =>import("@/components/ChartA.vue")
);
</script>

方法二:vue3+vite5 中


<template><AsyncTwo/>
</template><script setup>
import { defineAsyncComponent, ref } from "vue";const AsyncTwo = ref(null);
AsyncTwo.value = registerComponent("/ChartC");// 使用异步组件的方式加载组件
const registerComponent = (componentPath) => {const modules = import.meta.glob("./components/**/*.{vue,tsx}");for (const item in modules) {if (item.includes(componentPath)) {return defineAsyncComponent(modules[item]);}}
};
</script>
异步加载动态组件
<template><div v-for="(item, index) in componentsInfo" :key="index"><component :is="item.loadComp" /></div>
</template><script setup>
import { defineAsyncComponent, onMounted, ref } from "vue";const componentsInfo = ref([{id: "1-1",title: "图表A",component: "/ChartA",},{id: "1-2",title: "图表B",component: "/ChartB",},{id: "1-3",title: "图表C",component: "/ChartC",},
]);onMounted(() => {processLoad(componentsInfo.value);
});const processLoad = (info) => {for (let i = 0; i < info.length; i++) {let item = info[i];if (!item.component) {continue;}let resComp = registerComponent(item.component);item.loadComp = resComp;}
};// 注册一个异步组件
const registerComponent = (componentPath) => {//获取 src/components 文件夹下所有组件const modules = import.meta.glob("./components/**/*.{vue,tsx}"); for (const item in modules) {if (item.includes(componentPath)) {return defineAsyncComponent(modules[item]);}}
};
</script>

效果:
在这里插入图片描述

3、

相关文章:

  • 一种最大重叠离散小波包特征提取和支持向量机的ECG心电信号分类方法(MATLAB 2018)
  • 【UE5.1 角色练习】08-物体抬升、抛出技能 - part2
  • shell脚本:将一维数组以二维数组显示
  • 设计模式 18 迭代器模式 Iterator Pattern
  • 【QT八股文】系列之篇章2 | QT的信号与槽机制及通讯流程
  • vb.net,C#强制结束进程,“优雅”的退出方式
  • MyBatisPlus学习笔记(二)
  • 【STL】C++ stack(栈) 基本使用
  • 你真的会使用Vue3的onMounted钩子函数吗?Vue3中onMounted的用法详解
  • Wpf 使用 Prism 实战开发Day28
  • 第三方软件测试机构进行验收测试的好处分享,需多少时间和费用?
  • python -【一】基础语法
  • 计算机字符编码的发展
  • 机器学习(五) -- 监督学习(4) -- 集成学习方法-随机森林
  • CSP垦田计划
  • [分享]iOS开发 - 实现UITableView Plain SectionView和table不停留一起滑动
  • 【JavaScript】通过闭包创建具有私有属性的实例对象
  • Docker 1.12实践:Docker Service、Stack与分布式应用捆绑包
  • jQuery(一)
  • nginx 负载服务器优化
  • Python连接Oracle
  • 等保2.0 | 几维安全发布等保检测、等保加固专版 加速企业等保合规
  • 分享一个自己写的基于canvas的原生js图片爆炸插件
  • 后端_ThinkPHP5
  • 技术攻略】php设计模式(一):简介及创建型模式
  • 七牛云 DV OV EV SSL 证书上线,限时折扣低至 6.75 折!
  • 世界上最简单的无等待算法(getAndIncrement)
  • 线上 python http server profile 实践
  • 学习JavaScript数据结构与算法 — 树
  • 由插件封装引出的一丢丢思考
  • nb
  • 【运维趟坑回忆录】vpc迁移 - 吃螃蟹之路
  • 阿里云API、SDK和CLI应用实践方案
  • #NOIP 2014#day.2 T1 无限网络发射器选址
  • (2022 CVPR) Unbiased Teacher v2
  • (含react-draggable库以及相关BUG如何解决)固定在左上方某盒子内(如按钮)添加可拖动功能,使用react hook语法实现
  • (机器学习-深度学习快速入门)第三章机器学习-第二节:机器学习模型之线性回归
  • (三)Hyperledger Fabric 1.1安装部署-chaincode测试
  • (转)AS3正则:元子符,元序列,标志,数量表达符
  • (转)mysql使用Navicat 导出和导入数据库
  • .gitignore文件—git忽略文件
  • .mat 文件的加载与创建 矩阵变图像? ∈ Matlab 使用笔记
  • .NET Framework 服务实现监控可观测性最佳实践
  • .Net mvc总结
  • .net 怎么循环得到数组里的值_关于js数组
  • .NET 直连SAP HANA数据库
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地定义和使用弱事件
  • .Net通用分页类(存储过程分页版,可以选择页码的显示样式,且有中英选择)
  • .net图片验证码生成、点击刷新及验证输入是否正确
  • .Net转Java自学之路—SpringMVC框架篇六(异常处理)
  • //解决validator验证插件多个name相同只验证第一的问题
  • @德人合科技——天锐绿盾 | 图纸加密软件有哪些功能呢?
  • [10] CUDA程序性能的提升 与 流
  • [Android Studio] 开发Java 程序
  • [android] 看博客学习hashCode()和equals()