Vue3 父传子、使用 defineAsyncComponent 异步挂载组件、利用 is 动态引入组件
1、父组件 index.vue
defineAsyncComponent 是 Vue3 的异步挂载
<template><div v-for="v in nav_items" :key="v.text"><son :nav_text="v.text" :nav_component="v.nav_component"></son></div>
</template><script setup>
import { defineAsyncComponent, reactive } from "vue";// 子组件
import son from "./son.vue";let nav_items = reactive([{'text': '文字','nav_component': defineAsyncComponent(() => import("@/components/grandson.vue")) // 孙组件}
])
</script>
2、子组件 son.vue
使用 is 动态引入组件,用法:
<component :is="grandson"></component>
最终会变成:
<grandson></grandson>
<template><div class="level1_item"><span class="level1_text">{{props.nav_text}}</span><!-- 动态引入组件 --><component :is="props.nav_component"></component></div>
</template><script setup>
import { defineProps } from "vue";// 父组件传入的 导航第一级 图片及文字
let props = defineProps({nav_text: {type: String,default: () => {return null;}},nav_component: {type: Object, // ⚠️注意是对象default: () => {return null;}},
})
</script>
3、孙组件 grandson.vue
<template><div>123</div>
</template><script setup></script><style lang="scss" scoped>
</style>
⚠️注意
若出现警告(如下图):
Vue received a Component which was made a reactive object. This can lead to unnecessary performance overhead, and should be avoided by marking the component with `markRaw` or using `shallowRef` instead of `ref`.
Component that was made reactive: {name: 'AsyncComponentWrapper', __asyncLoader: ƒ, setup: ƒ}
解决方案 :
在第1步父组件 index.vue 里引入 shallowRef :
import { shallowRef } from "vue";
然后将 nav_component 修改为:
nav_component: shallowRef(defineAsyncComponent(() => import("@/components/grandson.vue")))
Vue3 父传子、使用 defineAsyncComponent 异步挂载组件、利用 is 动态引入组件相关推荐
- vue3 父传子 子传父
父传子 props接收数据 emit :子组件想使用父组件的方法 setup( props, { emit } ){ } index.vue(父组件) <CommonDialog v-if=& ...
- vue3父传子,子传父
<template><div><p>我是父组件</p><p>111111111111111</p><headrt :inf ...
- vue3组件通信的方式 父传子 props 和子传父 $emit 非父子组件的通信Provide和Inject 全局事件总线mitt库
vue3组件通信的方式 一.父传子 props 和子传父 $emit 1.父传子 props 在开发中最常见的就是父子组件之间通信,父组件往子组件通信使用的是props.子组件往父组件通信则使用的是 ...
- vue3.0 组件使用 引用 传值 父传子 子传父
vue3.0 组件使用 引用 传值 父传子 子传父 组件的创建 组件引入 在html中使用 组件传值 父传子 子接收参数 子传父 父接收参数 组件的创建 1.将所需功能封装在components目录下 ...
- vue3之组件通信 (props父传子,子传孙)(ts定义数组类型)
目录 vue3之组件通信 1 props父传子,子传孙 1-1 父组件 1-2 子组件 1-3 孙组件 2:父子传值 2:-1 父组件向子组件传值 Props 2-2 子组件向父组件传值 emit 3 ...
- Vue3 - 组件通信(父传子)
前言 在 Vue3 中,父组件向子组件传参的方法. 与 Vue2 相比,还是有一些区别的. 基础示例 现在我们的需求是,要通过父组件,传递一个标题来让子组件显示. 子组件 Com.vue: <t ...
- 3.vue3.2的父传子defineProps,子传父emits以及ref
1.父传子 <template><div class="container"><!-- 传递数据 这里传了一个string 和 一个list --&g ...
- 【vue3.0学习】父传子 子传父
父传子 <template><div id="app"><h1>父组件</h1><p>{{ money }}</p ...
- Vue3.2——父传子、子传父
## 父传子 父组件: <template><div class="home"><test-com :info="msg" tim ...
最新文章
- docker 删除镜像报错 image is referenced in multiple repositories
- python自动配置文件_【python接口自动化】- ConfigParser配置文件的使用
- java for循环前面label_Java 实例
- ssas脚本组织程序_脚本调试编辑
- 服务器克隆机网络端口排错
- 记前端知识--数组元素的相关操作
- 如何做一个淘宝客(前期为自己省钱)
- python分词工具
- Javaweb面试题整理
- 最大传输单元:MTU
- Control.DataBinding数据绑定细解
- android查ip地址,安卓手机查看IP地址的两种方法, 二、进入手机状态
- 新兴媒体舆情传播动态实时监测的技术解决方案
- riscv 开发板 HiFive Unmatched 总览
- 分布式下如何实现统一日志系统?
- Unity3D FPS射击游戏
- 计算机软件专业可以考哪些证书
- IPad苹果公司旗下平板电脑系列
- qt制作棋牌游戏之XO棋(井字棋)
- Nginx的动静分离实验