vue3 defineComponent + 渲染函数h + 全局注册

defineComponent 是 Vue 3 中的一个函数,用于定义一个组件。它是 Vue 3 的组合式 API 的一部分,提供了一种更加灵活和组织化的方式来定义组件。在 Vue 2 中,我们通常使用一个对象来定义组件,而在 Vue 3 中,defineComponent 函数提供了更多的类型支持和更好的集成。
defineComponent 的作用

    类型推断: 在使用 TypeScript 时,defineComponent 提供了更好的类型推断。它可以帮助 TypeScript 更准确地推断组件选项中的类型,比如 props、data、computed 等。

    组合式 API 支持: 它允许你使用 Vue 3 的组合式 API,这包括 ref、reactive、computed、watch 等响应式特性。

    更好的工具集成: defineComponent 为 Vue 工具(如 Vue Devtools)提供了更好的集成,使得组件的调试和维护更加容易。

    可选的模板编译: 当使用 defineComponent 时,你可以选择性地包含模板字符串,并且这些模板会在构建时自动编译。

 

https://cn.vuejs.org/guide/typescript/overview.html#definecomponent

import { defineComponent } from 'vue'

export default defineComponent({
  // 启用了类型推导
  props: {
    message: String
  },
  setup(props) {
    props.message // 类型:string | undefined
  }
})

https://cn.vuejs.org/api/render-function.html#h

https://cn.vuejs.org/guide/components/registration.html#global-registration

Demo

index.ts:

import { defineComponent, ref, h } from 'vue'

const ProTable = defineComponent({
  name: 'ProTable',
  props: {
    title: String
  },
  setup(props) {
    const count = ref(0)
    console.log(props)
    return () => {
      return h('div', `${count.value},${props.title}`)
    }
  }
})

export { ProTable }

main.ts:

import './assets/main.css'

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import WujieVue from 'wujie-vue3'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

import App from './App.vue'
import router from './router'
import { ProTable } from './components/light/pro-table/'

const app = createApp(App)

app.use(createPinia())
app.use(router)
app.use(WujieVue)
app.use(ElementPlus)
app.component('pro-table', ProTable)

app.mount('#app')

Index.vue:

<script setup>
import { onMounted, ref } from 'vue'
import './index.css'

onMounted(() => {})
</script>

<template>
  <div>
    <div>1</div>
    <pro-table title="标题"></pro-table>
  </div>
</template>

人工智能学习网站

https://chat.xutongbao.top

 

相关推荐

  1. vue3注册全局组件

    2024-06-12 17:58:02       39 阅读
  2. Vue 批量注册全局组件

    2024-06-12 17:58:02       56 阅读
  3. Vue.component v2v3注册(局部与全局)组件使用详解

    2024-06-12 17:58:02       30 阅读
  4. vueh() 函数

    2024-06-12 17:58:02       33 阅读
  5. 记录vue3h函数的各种使用方式

    2024-06-12 17:58:02       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-12 17:58:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-12 17:58:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-12 17:58:02       87 阅读
  4. Python语言-面向对象

    2024-06-12 17:58:02       96 阅读

热门阅读

  1. ######## redis各章节终篇索引(更新中) ############

    2024-06-12 17:58:02       33 阅读
  2. chrome 您的连接不是私密连接

    2024-06-12 17:58:02       28 阅读
  3. VS Code1.90发布,VS Code speech与AI结合太强了

    2024-06-12 17:58:02       22 阅读
  4. Python自学入门:全面解析与实战指南

    2024-06-12 17:58:02       28 阅读
  5. Matlab笔记

    2024-06-12 17:58:02       33 阅读
  6. 爬山算法的详细介绍

    2024-06-12 17:58:02       30 阅读
  7. 探索WebDriver:浏览器自动化的幕后英雄

    2024-06-12 17:58:02       39 阅读