亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vite5+vue3+?import.meta.glob動態(tài)導(dǎo)入vue組件圖文教程

 更新時間:2024年07月15日 10:47:37   作者:volodyan  
import.meta.glob是Vite提供的一個特殊功能,它允許你在模塊范圍內(nèi)動態(tài)地導(dǎo)入多個模塊,這篇文章主要給大家介紹了關(guān)于vite5+vue3+?import.meta.glob動態(tài)導(dǎo)入vue組件的相關(guān)資料,需要的朋友可以參考下

前言

import.meta.glob 是 Vite 提供的一個特殊功能,它允許你在模塊范圍內(nèi)動態(tài)地導(dǎo)入多個模塊。這在處理大量的文件,如組件、頁面或其他模塊時特別有用,特別是當(dāng)你需要根據(jù)某些條件或模式來動態(tài)加載它們時。

創(chuàng)建需要動態(tài)導(dǎo)入的組件目錄

假設(shè)你有一個src/pages/DynamicComponents目錄,里面包含多個 Vue 組件,你想根據(jù)某些條件動態(tài)地導(dǎo)入這些組件。

首先,確保你的目錄結(jié)構(gòu)是這樣的:

src/pages/index.vue文件

<template>
    <div class="DynamicComponentsOutbox">
        <h1>DynamicComponents</h1>
    </div>
    <div>
        <component v-for="(component, name) in dynamicComponents" :key="name" :is="component" />
    </div>
</template>

<script setup>
import { onMounted, reactive,markRaw } from 'vue'
const dynamicComponents = reactive({})
onMounted(async () => {
    const modules = import.meta.glob('./DynamicComponents/*.vue');

    for (const path in modules) {
        const module = await modules[path]();
        const componentName = path.replace(/^\.\/components\/(.*)\.vue$/, '$1');
        console.log(`componentName`,componentName );
      dynamicComponents[componentName] = markRaw(module.default)
     
    }
    console.log(`dynamicComponents`,dynamicComponents);
})
</script>

<style lang="scss" scoped></style>

 src/pages/DynamicComponents/ComponentA.vue文件

<template>
    <div class="">
        <h1>ComponentA</h1>
    </div>
</template>
  
<script setup>

</script>
  
<style lang="scss" scoped></style>

 src/pages/DynamicComponents/ComponentB.vue文件

<template>
    <div class="">
        <h1>ComponentB</h1>
    </div>
  </template>
  
  <script setup>
  
  </script>
  
  <style lang="scss" scoped>
  
  </style>

 App.vue文件

<template>
  <main>
    <HelloWorld msg="You did it!" />
    <component :is='DynamicComponents'></component>
  </main>
</template>
<script setup>
import HelloWorld from '@/components/HelloWorld.vue'
import DynamicComponents from '@/pages/index.vue'
</script>
<style scoped></style>

index.vue:6 
 [Vue warn]: Vue received a Component that was made a reactive object. This can lead to unnecessary performance overhead and should be avoided by marking the component with `markRaw` or using `shallowRef` instead of `ref`. 
Component that was made reactive:  
{__hmrId: 'ed2b2297', __file: 'C:/Users/63002/Desktop/codefile/vue3-vite2/src/pages/DynamicComponents/ComponentA.vue', render: ?}
 
  at <Index> 
  at <App>

剛開始這樣寫 

 dynamicComponents[componentName] =module.default 

這里報了一個警告:提示你去給組件使用markRaw or shallowRef包起來就好了,我直接使用了markRaw來包起組件 ,就解決這個警告了。

改為:

import { onMounted, reactive,markRaw } from 'vue'
dynamicComponents[componentName] = markRaw(module.default)
<template>
    <div class="DynamicComponentsOutbox">
        <h1>DynamicComponents</h1>
    </div>
    <div>
        <component v-for="(component, name) in dynamicComponents" :key="name" :is="component" />
    </div>
</template>

<script setup>
import { onMounted, reactive,markRaw } from 'vue'
const dynamicComponents = reactive({})
onMounted(async () => {
    const modules = import.meta.glob('./DynamicComponents/*.vue');
    for (const path in modules) {
        const module = await modules[path]();
        const componentName = path.replace(/^\.\/components\/(.*)\.vue$/, '$1');
        console.log(`componentName`,componentName );
      dynamicComponents[componentName] = markRaw(module.default)
     
    }
    console.log(`dynamicComponents`,dynamicComponents);
})
</script>

<style lang="scss" scoped></style>

在上面的代碼中,我們首先在onMounted鉤子中使用 import.meta.glob 獲取 components 目錄下所有 .vue 文件的動態(tài)導(dǎo)入。然后,我們遍歷這些模塊,加載它們,并將它們存儲在 dynamicComponents 對象中,其中鍵是組件的名稱(從文件路徑中提?。凳墙M件的默認(rèn)導(dǎo)出。

最后,在模板中,我們使用 v-for 指令遍歷 dynamicComponents 對象,并使用 Vue 的動態(tài)組件功能 (<component :is="...">) 來渲染每個組件。

注意:由于 import.meta.glob 是在構(gòu)建時解析的,因此它不會為動態(tài)路徑或運行時確定的路徑工作。它主要用于靜態(tài)路徑模式。 

總結(jié) 

到此這篇關(guān)于vite5+vue3+ import.meta.glob動態(tài)導(dǎo)入vue組件的文章就介紹到這了,更多相關(guān)import.meta.glob動態(tài)導(dǎo)入vue組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue uniapp 防止按鈕多次點擊的三種實現(xiàn)方式

    vue uniapp 防止按鈕多次點擊的三種實現(xiàn)方式

    最近的項目完成后,在性能優(yōu)化階段需要做按鈕的防止重復(fù)點擊功能,本文主要介紹了vue uniapp 防止按鈕多次點擊的三種實現(xiàn)方式,具有一定的參考價值,感興趣的可以了解一下
    2023-08-08
  • Vue+Element實現(xiàn)封裝抽屜彈框

    Vue+Element實現(xiàn)封裝抽屜彈框

    這篇文章主要為大家詳細(xì)介紹了如何利用Vue和Element實現(xiàn)簡單的抽屜彈框效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-06-06
  • Vue3啟用gzip壓縮nginx配置詳解

    Vue3啟用gzip壓縮nginx配置詳解

    這篇文章主要為大家介紹了Vue3啟用gzip壓縮時nginx配置gzip示例詳解詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Vue動態(tài)擴(kuò)展表頭的表格及數(shù)據(jù)方式(數(shù)組嵌套對象)

    Vue動態(tài)擴(kuò)展表頭的表格及數(shù)據(jù)方式(數(shù)組嵌套對象)

    這篇文章主要介紹了Vue動態(tài)擴(kuò)展表頭的表格及數(shù)據(jù)方式(數(shù)組嵌套對象),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • ElementUI 修改默認(rèn)樣式的幾種辦法(小結(jié))

    ElementUI 修改默認(rèn)樣式的幾種辦法(小結(jié))

    這篇文章主要介紹了ElementUI 修改默認(rèn)樣式的幾種辦法(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 如何用Vite構(gòu)建工具快速創(chuàng)建Vue項目

    如何用Vite構(gòu)建工具快速創(chuàng)建Vue項目

    Vite是一個web開發(fā)構(gòu)建工具,由于其原生?ES?模塊導(dǎo)入方法,它允許快速提供代碼,下面這篇文章主要給大家介紹了關(guān)于如何用Vite構(gòu)建工具快速創(chuàng)建Vue項目的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • Vue自定義詢問彈框和輸入彈框的示例代碼

    Vue自定義詢問彈框和輸入彈框的示例代碼

    這篇文章主要介紹了Vue自定義詢問彈框和輸入彈框,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-06-06
  • antd Select下拉菜單動態(tài)添加option里的內(nèi)容操作

    antd Select下拉菜單動態(tài)添加option里的內(nèi)容操作

    這篇文章主要介紹了antd Select下拉菜單動態(tài)添加option里的內(nèi)容操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Vue終端取消vue、prettier警告warn問題

    Vue終端取消vue、prettier警告warn問題

    這篇文章主要介紹了Vue終端取消vue、prettier警告warn問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • 使用Mockjs模擬接口實現(xiàn)增刪改查、分頁及多條件查詢

    使用Mockjs模擬接口實現(xiàn)增刪改查、分頁及多條件查詢

    Mock.js是一個模擬數(shù)據(jù)生成器,可以讓前端獨立于后端進(jìn)行開發(fā),下面這篇文章主要給大家介紹了關(guān)于使用Mockjs模擬接口實現(xiàn)增刪改查、分頁及多條件查詢的相關(guān)資料,需要的朋友可以參考下
    2022-04-04

最新評論