vue3中動態(tài)組件的踩坑記錄分享
1.動態(tài)組件綁定問題
上周用vue3的動態(tài)組件替換了v-fo循環(huán)的頁面,在實際中遇到了一個頭疼的問題。組件單獨寫是可以出來,但使用動態(tài)組件卻提示組件未注冊
組件未注冊
<template>
<div>
<div class="top text-center">
<el-radio-group v-model="currentMode" size="medium">
<el-radio-button v-for="(item, i) in menuList" :key="item.value" :label="item.value">
{{ item.label }}
</el-radio-button>
</el-radio-group>
</div>
<component :is="currentMode"></component>
</div>
</template>
?
<script setup>
import Plan1 from './plan1.vue';
import Plan2 from './plan2.vue';
import Plan3 from './plan3.vue';
?
?
import { ref } from 'vue';
const menuList = [
{ label: 'Plan1', value: 'Plan1' },
{ label: 'Plan2', value: 'Plan2' },
{ label: 'Plan3', value: 'Plan3' },
];
const currentMode = ref(menuList[0].value);
</script>
?
<style lang="scss" scoped></style>
報錯信息
vue 3 Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the "name" option.
我以為是setup 寫法 name丟失的問題
在百度上看到最多的還是將“name”屬性默認導(dǎo)出,嘗試了也無濟于事。
就去仔細,查了下vue文檔

官方建議使用三元表達式來實現(xiàn)動態(tài)組件:is="someCondition ? Foo : Bar"
解決方案
更具這個思路,那就很好處理了,只要將currentMode的值設(shè)置為elevator、airCond不就好了,搞個鍵值對映射就可以搞定了
function getComponent() {
const component = {
Plan1: Plan1,
Plan2: Plan2,
Plan3: Plan3,
};
return component[loadCountFlag.value];
}
最后將 getComponent()賦值給動態(tài)組件,刷新頁面查看控制臺,頁面無報錯

如果你的動態(tài)組件過多,可以使用defineAsyncComponent來實現(xiàn)組件懶加載,類似vue2中箭頭函數(shù)的寫法Plan1: () =>import('./Plan1.vue'),
<template>
<component :is="getComponent()" />
</template>
?
<script setup>
import { ref, defineAsyncComponent } from 'vue';
?
// Assuming loadCountFlag is a reactive reference
const loadCountFlag = ref(1);
?
// Lazy-load components
const Plan1 = defineAsyncComponent(() => import('./components/Plan1.vue'));
const Plan2 = defineAsyncComponent(() => import('./components/Plan2.vue'));
const Plan3 = defineAsyncComponent(() => import('./components/Plan3.vue'));
?
function getComponent() {
const componentMap = {
1: Plan1,
2: Plan2,
3: Plan3,
};
return componentMap[loadCountFlag.value];
}
</script>
2.動態(tài)組件的方法導(dǎo)出問題
動態(tài)組件解決了,之前每個組件還有一個數(shù)據(jù)保存的方法,需要在父頁面自動調(diào)用,并暴漏出去

于是這就很簡單嗎,直接使用vue2中this.$refs.xxx.xxx不就好了
正當(dāng)我高高興興提交完代碼準(zhǔn)備下班時,測試的同事把我叫住了說自動保存沒觸發(fā),數(shù)據(jù)丟失了
問了了GPT:

打開vue工具,組件切換檢查了下,發(fā)現(xiàn)確實在變

于是我嘗試著打印currentComponent.value?.saveData方法,控制臺出現(xiàn)了。但是上層打印的確實undefind
組件切換的時候,autoSave應(yīng)該動態(tài)切換,autoSave應(yīng)該時響應(yīng)式的,那么問題就解決了
解決辦法:
使用computed包裹一層

問題解決,下班!
到此這篇關(guān)于vue3中動態(tài)組件的踩坑記錄分享的文章就介紹到這了,更多相關(guān)vue3動態(tài)組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue-vuex中使用commit提交mutation來修改state的方法詳解
今天小編就為大家分享一篇vue-vuex中使用commit提交mutation來修改state的方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
electron-vue利用webpack打包實現(xiàn)多頁面的入口文件問題
項目需要在electron的項目中新打開一個窗口,利用webpack作為靜態(tài)資源打包器,發(fā)現(xiàn)在webpack中可以設(shè)置多頁面的入口,今天來講一下我在electron中利用webpack建立多頁面入口的踩坑經(jīng)驗,需要的朋友可以參考下2019-05-05
vue.config.js中配置configureWebpack和chainWebpack以及一些常用的配置
configureWebpack和chainWebpack都是Vue CLI中用于修改Webpack配置的工具,configureWebpack可以通過對象或函數(shù)修改配置,簡單直接;chainWebpack則使用WebpackChainAPI,適合復(fù)雜配置,兩者可以結(jié)合使用,以達到更精細的配置需求,幫助開發(fā)者優(yōu)化項目構(gòu)建2024-10-10
Vue項目導(dǎo)入導(dǎo)出文件功能以及導(dǎo)出文件后亂碼問題及解決
這篇文章主要介紹了Vue項目導(dǎo)入導(dǎo)出文件功能以及導(dǎo)出文件后亂碼問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
Vue源碼學(xué)習(xí)之?dāng)?shù)據(jù)初始化
這篇文章主要為大家介紹了Vue源碼學(xué)習(xí)之?dāng)?shù)據(jù)初始化實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
Vue+Element ui實現(xiàn)樹形控件右鍵菜單
這篇文章主要為大家詳細介紹了Vue+Element ui實現(xiàn)樹形控件右鍵菜單,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07

