Vue3中局部組件和全局組件的使用教程詳解
1. 局部組件
Card.vue
<template> <div class="card"> <header> <div>標(biāo)題</div> <div>副標(biāo)題</div> </header> <section>內(nèi)容</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 注冊(cè)單個(gè)全局組件
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 批量注冊(cè)全局組件
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']; // 使用動(dòng)態(tài)導(dǎo)入的方式注冊(cè)全局組件時(shí)需要注意異步組件的加載 // 異步組件使用 defineAsyncComponent 方法來(lái)處理動(dòng)態(tài)導(dǎo)入的組件,并且使用 await 關(guān)鍵字等待組件的加載完成。在組件加載完成后,再將其注冊(cè)為全局組件。 // 如果沒(méi)有注意異步組件的加載,會(huì)報(bào) Invalid VNode type: undefined (undefined) 的問(wèn)題 async function registerComponents() { for (const componentName of componentNames) { // 使用 defineAsyncComponent 方法動(dòng)態(tài)導(dǎo)入異步組件 const component = await defineAsyncComponent(() => import(`./components/${componentName}.vue`)); app.component(componentName, component); } app.mount('#app'); } registerComponents();
到此這篇關(guān)于Vue3中局部組件和全局組件的使用教程詳解的文章就介紹到這了,更多相關(guān)Vue3組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue+openlayers+nodejs+postgis實(shí)現(xiàn)軌跡運(yùn)動(dòng)效果
使用postgres(postgis)數(shù)據(jù)庫(kù)以及nodejs作為后臺(tái),vue和openlayers做前端,openlayers使用http請(qǐng)求通過(guò)nodejs從postgres數(shù)據(jù)庫(kù)獲取數(shù)據(jù),這篇文章主要介紹了vue+openlayers+nodejs+postgis實(shí)現(xiàn)軌跡運(yùn)動(dòng),需要的朋友可以參考下2024-05-05關(guān)于VueRouter導(dǎo)入的全過(guò)程
這篇文章主要介紹了關(guān)于VueRouter導(dǎo)入的全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08element validate驗(yàn)證函數(shù)不執(zhí)行的原因分析
這篇文章主要介紹了element validate驗(yàn)證函數(shù)不執(zhí)行的原因分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04vue項(xiàng)目打包部署到nginx后css樣式失效的問(wèn)題及解決方法
我將自己的前端Vue項(xiàng)目,經(jīng)過(guò)build生成的dist文件夾copy到nginx的html文件夾中,然后寫(xiě)了配置文件,運(yùn)行訪問(wèn)后發(fā)現(xiàn)頁(yè)面css樣式?jīng)]有加載到,下面給大家介紹vue項(xiàng)目打包部署到nginx后css樣式失效的問(wèn)題及解決方法,感興趣的朋友一起看看吧2024-12-12element-plus?el-form表單驗(yàn)證使用方法以及注意事項(xiàng)
這篇文章主要給大家介紹了關(guān)于element-plus?el-form表單驗(yàn)證使用方法以及注意事項(xiàng)的相關(guān)資料,表單驗(yàn)證能通過(guò)設(shè)置驗(yàn)證規(guī)則驗(yàn)證用戶的輸入,并對(duì)不規(guī)范的輸入做出對(duì)應(yīng)提示,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12