Vue3中局部组件和全局组件的使用教程详解

 更新时间:2023年07月30日 15:38:09   作者:谢尔登  
这篇文章主要为大家学习介绍了Vue3中局部组件和全局组件的使用方法,文中的示例代码讲解详细,具有一定的借鉴价值,需要的小伙伴可以学习一下

1. 局部组件

Card.vue

<template>
  <div class="card">
    <header>
      <div>标题</div>
      <div>副标题</div>
    </header>
    <section>内容</section>
  </div>
</template>
<script setup lang="ts">
</script>
<style lang="less" scoped>
@border: #ccc;
.card {
  border: 1px solid @border;
  width: 400px;
  header {
    display: flex;
    justify-content: space-between;
    padding: 5px;
    border-bottom: 1px solid @border;
  }
  section{
    padding: 5px;
    min-height: 300px;
  }
}
</style>

App.vue

<template>
  <div>
    <CardVue></CardVue>
  </div>
</template>
<script setup lang="ts">
import CardVue from './components/Card.vue'
</script>
<style lang="scss" scoped>
</style>

2. 全局组件

2.1 注册单个全局组件

Card.vue

同上

App.vue

<template>
  <div>
    <Card></Card>
  </div>
</template>
<script setup lang="ts">
</script>
<style lang="scss" scoped></style>

main.ts

import { createApp } from 'vue'
import App from './App.vue'
import CardVue from './components/Card.vue'
export const app = createApp(App)
app.component('Card', CardVue)
app.mount('#app')

2.2 批量注册全局组件

Card.vue

同上

Tree.vue

<template>
  <div>
      <h1>this is a title</h1>
  </div>
</template>
<script setup lang="ts">
</script>
<style lang="scss" scoped>
h1 {
  border: 1px solid black;
}
</style>

App.vue

<template>
  <div>
    <Card></Card>
    <Tree></Tree>
  </div>
</template>
<script setup lang="ts">
</script>
<style lang="scss" scoped></style>

main.ts

import { createApp, defineAsyncComponent } from 'vue'
import App from './App.vue'
const app = createApp(App)
const componentNames = ['Card', 'Tree'];
// 使用动态导入的方式注册全局组件时需要注意异步组件的加载
// 异步组件使用 defineAsyncComponent 方法来处理动态导入的组件,并且使用 await 关键字等待组件的加载完成。在组件加载完成后,再将其注册为全局组件。
// 如果没有注意异步组件的加载,会报 Invalid VNode type: undefined (undefined) 的问题
async function registerComponents() {
  for (const componentName of componentNames) {
    // 使用 defineAsyncComponent 方法动态导入异步组件
    const component = await defineAsyncComponent(() => import(`./components/${componentName}.vue`));
    app.component(componentName, component);
  }
  app.mount('#app');
}
registerComponents();

到此这篇关于Vue3中局部组件和全局组件的使用教程详解的文章就介绍到这了,更多相关Vue3组件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue+openlayers+nodejs+postgis实现轨迹运动效果

    vue+openlayers+nodejs+postgis实现轨迹运动效果

    使用postgres(postgis)数据库以及nodejs作为后台,vue和openlayers做前端,openlayers使用http请求通过nodejs从postgres数据库获取数据,这篇文章主要介绍了vue+openlayers+nodejs+postgis实现轨迹运动,需要的朋友可以参考下
    2024-05-05
  • 微信小程序使用uni-app一键获取用户信息

    微信小程序使用uni-app一键获取用户信息

    这篇文章主要介绍了微信小程序使用uni-app一键获取用户信息,需要的朋友可以参考下
    2023-01-01
  • Vue3项目中的hooks的使用教程

    Vue3项目中的hooks的使用教程

    今天我们稍微说一下 vue3 项目中的 hooks 的使用,其实这个 hooks 呢是和 vue2 当中的 mixin 是类似的,学习过 vue2 的小伙伴一定对 mixin 一定比较熟悉,快跟随小编一起来学习学习吧
    2022-08-08
  • 关于Vue3使用axios的配置教程详解

    关于Vue3使用axios的配置教程详解

    道axios是一个库,并不是vue中的第三方插件,下面这篇文章主要给大家介绍了关于Vue3使用axios的配置教程,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-04-04
  • 关于VueRouter导入的全过程

    关于VueRouter导入的全过程

    这篇文章主要介绍了关于VueRouter导入的全过程,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • element validate验证函数不执行的原因分析

    element validate验证函数不执行的原因分析

    这篇文章主要介绍了element validate验证函数不执行的原因分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-04-04
  • 详细分析vue表单数据的绑定

    详细分析vue表单数据的绑定

    这篇文章主要介绍了vue表单数据的绑定的相关资料,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • vue中引用阿里字体图标的方法

    vue中引用阿里字体图标的方法

    这篇文章主要介绍了vue中引用阿里字体图标出现错误问题的解决方法,感兴趣的朋友跟随脚本之家小编一起学习吧
    2018-02-02
  • vue项目打包部署到nginx后css样式失效的问题及解决方法

    vue项目打包部署到nginx后css样式失效的问题及解决方法

    我将自己的前端Vue项目,经过build生成的dist文件夹copy到nginx的html文件夹中,然后写了配置文件,运行访问后发现页面css样式没有加载到,下面给大家介绍vue项目打包部署到nginx后css样式失效的问题及解决方法,感兴趣的朋友一起看看吧
    2024-12-12
  • element-plus el-form表单验证使用方法以及注意事项

    element-plus el-form表单验证使用方法以及注意事项

    这篇文章主要给大家介绍了关于element-plus el-form表单验证使用方法以及注意事项的相关资料,表单验证能通过设置验证规则验证用户的输入,并对不规范的输入做出对应提示,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-12-12

最新评论