Vue3動(dòng)態(tài)組件component不生效問(wèn)題解決方法
問(wèn)題: vue3循環(huán)渲染動(dòng)態(tài)組件component不生效,頁(yè)面空白
在vue3使用component動(dòng)態(tài)組件展示組件時(shí),組件就是不展示顯示空白。在vue2中使用動(dòng)態(tài)變量component展示組件都是沒(méi)問(wèn)題。試了很多方法 踩了很多坑,所以記錄下:
<div class="preview-list" id="canvas-area">
<component
v-for="component in components"
:key="component.id"
:is="component.name"
v-bind="component.props"
/>
</div>
<script setup lang="ts">
import LText from '@/components/LText'
import { ref } from 'vue'
interface styleProps = {
text: string;
fontSize: string;
}
interface componentData = {
id: number;
name: string;
props?: styleProps;
}
const components = ref<componentData[]>([
{ id: 1, name: 'LText', props: { text: 'hello', fontSize: '12px'}},
{ id: 2, name: 'LText', props: { text: 'hello2', fontSize: '14px'}},
{ id: 3, name: 'LText', props: { text: 'hello3', fontSize: '16px'}}
])
</script>
因?yàn)関ue3使用的是setup語(yǔ)法,組件只要import導(dǎo)入就行 不需要再像vue2中在components掛載,這樣導(dǎo)致我想渲染的組件是沒(méi)有渲染出來(lái)頁(yè)面出現(xiàn)空白,嘗試了很多辦法對(duì)應(yīng)的組件里面添加多個(gè)script指定對(duì)應(yīng)的組件名,還是沒(méi)生效
解決方法
使用shallowReactive或者shallowRef把對(duì)應(yīng)的組件名稱(chēng)重新定義下,遍歷component時(shí),is采用對(duì)象key獲取對(duì)應(yīng)的對(duì)應(yīng)的組件,這樣組件就顯示出來(lái)了
<div class="preview-list" id="canvas-area">
<component
v-for="component in components"
:key="component.id"
:is="componentsName[component.name]"
v-bind="component.props"
/>
</div>
<script setup lang="ts">
import LText from '@/components/LText'
import { ref, shallowReactive } from 'vue'
interface styleProps = {
text: string;
fontSize: string;
}
interface componentData = {
id: number;
name: string;
props?: styleProps;
}
type componentName = {
[key: string]: any
}
const components = ref<componentData[]>([
{ id: 1, name: 'LText', props: { text: 'hello', fontSize: '12px'}},
{ id: 2, name: 'LText', props: { text: 'hello2', fontSize: '14px'}},
{ id: 3, name: 'LText', props: { text: 'hello3', fontSize: '16px'}}
])
// 解決方案
const componentsName = shallowReactive<componentName>({
LText
})
</script>
拓展:Vue3使用動(dòng)態(tài)組件 Component
一、動(dòng)態(tài)組件的概念
多個(gè)組件通過(guò)component標(biāo)簽掛載在同一個(gè)組件中,通過(guò)觸發(fā)動(dòng)作進(jìn)行動(dòng)態(tài)切換,常搭配<keep-alive></keep-alive>使用。
二、使用場(chǎng)景
多用于以下幾個(gè)場(chǎng)景:
1、tab欄的切換
管理系統(tǒng)中切換不同的菜單,展示tab,切換tab可以渲染不同組件,一般搭配<keep-alive></keep-alive>使用。
2、條件性地渲染組件
根據(jù)某個(gè)條件決定渲染哪個(gè)組件。通過(guò)在<component>元素上使用v-if指令來(lái)實(shí)現(xiàn)。
3、動(dòng)態(tài)切換組件
根據(jù)用戶的交互或狀態(tài)變化,切換顯示不同的組件。通過(guò)在<component>元素上使用is屬性來(lái)指定要渲染的組件。
4、異步加載組件
當(dāng)組件非常大或需要懶加載時(shí),可以使用動(dòng)態(tài)組件來(lái)異步加載組件,從而提高頁(yè)面加載速度。
5、與路由結(jié)合使用
在路由配置中使用動(dòng)態(tài)組件,根據(jù)不同的路由路徑加載相應(yīng)的組件。
三、使用示例
1、掛載組件
通過(guò)vue的defineAsyncComponent實(shí)現(xiàn)掛載組件
const CourseData = defineAsyncComponent(() => import("@/components/Chart/courseData.vue"));2、component的is屬性
<component :is="item.component" />
3、動(dòng)態(tài)組件傳值
動(dòng)態(tài)組件的傳值遵循基本組件傳值規(guī)則,除了支持v-bind傳值以外,還支持ref引用傳值;使用引用傳值需要注意的是,需要確定組件之后,再使用ref屬性進(jìn)行傳值,否則將會(huì)無(wú)法獲取應(yīng)用組件的屬性。使用v-bind傳值代碼如下所示:
<template>
<div>
<component :is="item.component" :data="reportData" :exam-data="exampData"/>
</div>
</template>
<script lang="ts" setup>
const CourseData = defineAsyncComponent(() => import("@/components/Chart/courseData.vue"));
const item = reactive({
component: CourseData
})
const reportData = ref("aaaaa")
const exampData = ref("bbbb")
</script>4、動(dòng)態(tài)組件數(shù)據(jù)緩存
若是數(shù)據(jù)需要?jiǎng)討B(tài)渲染,組件切換之后會(huì)導(dǎo)致之前獲得的數(shù)據(jù)丟失,這個(gè)時(shí)候,若我們想要在組件切換過(guò)程中保持這些組件的狀態(tài),避免重復(fù)渲染導(dǎo)致性能問(wèn)題,則可以使用<keep-alive></keep-alive>包裹動(dòng)態(tài)組件,來(lái)緩存組件中的數(shù)據(jù):
<template>
<div>
<div id="dynamic-component-demo" class="demo">
<button
v-for="tab in tabs"
:key="tab"
:class="['tab-button', { active: currentTab === tab }]"
@click="currentTab = tab"
>
{{ tab }}
</button>
<keep-alive>
<component
:is="item.component"
class="tab"
></component>
</keep-alive>
</div>
</div>
</template>到此這篇關(guān)于Vue3動(dòng)態(tài)組件component不生效問(wèn)題解決方法的文章就介紹到這了,更多相關(guān)Vue3 component不生效內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3通過(guò)父子傳值實(shí)現(xiàn)彈框功能
在Vue3中,我們可以通過(guò)?provide?和?inject?來(lái)實(shí)現(xiàn)父子組件之間的數(shù)據(jù)傳遞,這也適用于實(shí)現(xiàn)彈框功能,下面我們就來(lái)學(xué)習(xí)一下vue3實(shí)現(xiàn)彈框功能的具體方法吧2023-12-12
vue中router.beforeEach()的簡(jiǎn)單用法舉例
router.beforeEach()一般用來(lái)做一些進(jìn)入頁(yè)面的限制,比如沒(méi)有登錄,就不能進(jìn)入某些頁(yè)面,只有登錄了之后才有權(quán)限查看某些頁(yè)面,下面這篇文章主要給大家介紹了關(guān)于vue中router.beforeEach()的簡(jiǎn)單用法舉例,需要的朋友可以參考下2023-01-01
Vue使用<Suspense/>實(shí)現(xiàn)圖片加載組件
Suspense是Vue的內(nèi)置組件,用于管理異步組件的加載狀態(tài),包括等待、出錯(cuò)和最終渲染,它通過(guò)兩個(gè)插槽(default和fallback)來(lái)實(shí)現(xiàn)這一功能,感興趣的可以了解一下2026-01-01
淺談Vue render函數(shù)在ElementUi中的應(yīng)用
今天小編就為大家分享一篇淺談Vue render函數(shù)在ElementUi中的應(yīng)用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
Vue項(xiàng)目路由刷新的實(shí)現(xiàn)代碼
這篇文章主要介紹了Vue項(xiàng)目路由刷新的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04
Vue3如何解決路由緩存問(wèn)題(響應(yīng)路由參數(shù)的變化)
這篇文章主要介紹了Vue3如何解決路由緩存問(wèn)題(響應(yīng)路由參數(shù)的變化),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
vue3項(xiàng)目中eslint+prettier統(tǒng)一代碼風(fēng)格方式
這篇文章主要介紹了vue3項(xiàng)目中eslint+prettier統(tǒng)一代碼風(fēng)格方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-05-05
Vue.js組件tree實(shí)現(xiàn)省市多級(jí)聯(lián)動(dòng)
這篇文章主要為大家詳細(xì)介紹了Vue.js組件tree實(shí)現(xiàn)省市多級(jí)聯(lián)動(dòng)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12

