欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue3中局部組件和全局組件的使用教程詳解

 更新時(shí)間:2023年07月30日 15:38:09   作者:謝爾登  
這篇文章主要為大家學(xué)習(xí)介紹了Vue3中局部組件和全局組件的使用方法,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的小伙伴可以學(xué)習(xí)一下

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)效果

    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
  • 微信小程序使用uni-app一鍵獲取用戶信息

    微信小程序使用uni-app一鍵獲取用戶信息

    這篇文章主要介紹了微信小程序使用uni-app一鍵獲取用戶信息,需要的朋友可以參考下
    2023-01-01
  • Vue3項(xiàng)目中的hooks的使用教程

    Vue3項(xiàng)目中的hooks的使用教程

    今天我們稍微說(shuō)一下 vue3 項(xiàng)目中的 hooks 的使用,其實(shí)這個(gè) hooks 呢是和 vue2 當(dāng)中的 mixin 是類似的,學(xué)習(xí)過(guò) vue2 的小伙伴一定對(duì) mixin 一定比較熟悉,快跟隨小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • 關(guān)于Vue3使用axios的配置教程詳解

    關(guān)于Vue3使用axios的配置教程詳解

    道axios是一個(gè)庫(kù),并不是vue中的第三方插件,下面這篇文章主要給大家介紹了關(guān)于Vue3使用axios的配置教程,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • 關(guān)于VueRouter導(dǎo)入的全過(guò)程

    關(guān)于VueRouter導(dǎo)入的全過(guò)程

    這篇文章主要介紹了關(guān)于VueRouter導(dǎo)入的全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • element validate驗(yàn)證函數(shù)不執(zhí)行的原因分析

    element validate驗(yàn)證函數(shù)不執(zhí)行的原因分析

    這篇文章主要介紹了element validate驗(yàn)證函數(shù)不執(zhí)行的原因分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • 詳細(xì)分析vue表單數(shù)據(jù)的綁定

    詳細(xì)分析vue表單數(shù)據(jù)的綁定

    這篇文章主要介紹了vue表單數(shù)據(jù)的綁定的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • vue中引用阿里字體圖標(biāo)的方法

    vue中引用阿里字體圖標(biāo)的方法

    這篇文章主要介紹了vue中引用阿里字體圖標(biāo)出現(xiàn)錯(cuò)誤問(wèn)題的解決方法,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-02-02
  • vue項(xiàng)目打包部署到nginx后css樣式失效的問(wèn)題及解決方法

    vue項(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-12
  • element-plus?el-form表單驗(yàn)證使用方法以及注意事項(xiàng)

    element-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

最新評(píng)論