vue3 如何在 jsx中使用 component 组件

简介: component 组件是 vue 在模板中加载动态组件的方式,在 jsx 中可以使用if...else自己处理

component 组件不像其它的内置组件(tansitiontransitionGroup),可以直接从 vue 中直接导出,所有要在 jsx 使用component就要使用 h 函数

使用 vue 内置组件

// xxx.jsx
import { defineComponent, Transition } from 'vue';

export default defineComponent({
    name: "v-test",
    setup(props, { attrs, slots }){
        return () => <Transition><div>{ slots.default?.() }</div></Transition>
    }
})

使用动态组件-component

// xxx.jsx
import { defineComponent } from 'vue';

export default defineComponent({
    name: "v-test",
    props:{
        tag: {
            type: String,
            default: "div",
        }
    },
    setup(props, { attrs, slots }){
        return () => h(props.tag, {...attrs}, slots.default?.())
    }
})
相关文章
|
2天前
|
JavaScript 编译器 API
vue2和vue3区别
vue2和vue3区别
11 4
|
1天前
|
存储 JavaScript 前端开发
vue3获取本地的当前时间转化为年月日显示然后计算之后一周的时间
vue3获取本地的当前时间转化为年月日显示然后计算之后一周的时间
|
1天前
|
存储 JavaScript API
Vue 3 中实现引导页
Vue 3 中实现引导页
|
1天前
|
前端开发 JavaScript
vue3中覆盖组件样式的方法
vue3中覆盖组件样式的方法
|
2天前
|
JavaScript
vue组件中data为什么必须是一个函数?
vue组件中data为什么必须是一个函数?
7 1
|
3天前
|
JavaScript
解决vue3中不支持.sync语法糖
解决vue3中不支持.sync语法糖
9 3
|
3天前
|
JavaScript 前端开发 Unix
vue3毫秒级时间戳转换
该内容介绍了一个关于时间戳转换的工具包,包括将时间转换为时间戳和时间戳转换为时间的两个JavaScript函数。`Jh_convertTimeStamp`函数将日期字符串转换为时间戳,而`Jh_timeStampToTime`函数则将时间戳转换回指定格式的日期字符串。此外,还提到了一个在线时间戳转换工具的链接和一个名为Tools-Web的开源工具站,可用于搭建个人综合工具站。
|
10月前
|
JavaScript
Vue的非父子组件之间传值
全局事件总线 一种组件间通信的方式,适用于任意组件间通信
|
9月前
|
缓存 JavaScript 前端开发
Vue Props、Slot、v-once、非父子组件间的传值....
Vue Props、Slot、v-once、非父子组件间的传值....
50 0
|
10月前
|
JavaScript
Vue中父子组件传值
先在⽗组件中给⼦组件的⾃定义属性绑定⼀个⽗组件的变量