最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue3中動態(tài)組件的踩坑記錄分享

 更新時間:2024年12月11日 10:39:06   作者:本末倒置183  
這篇文章主要為大家詳細(xì)分享一下vue3中動態(tài)組件遇到的問題,分別是動態(tài)組件綁定問題和動態(tài)組件的方法導(dǎo)出問題,有需要的小伙伴可以參考一下

1.動態(tài)組件綁定問題

上周用vue3的動態(tài)組件替換了v-fo循環(huán)的頁面,在實(shí)際中遇到了一個頭疼的問題。組件單獨(dú)寫是可以出來,但使用動態(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”屬性默認(rèn)導(dǎo)出,嘗試了也無濟(jì)于事。

就去仔細(xì),查了下vue文檔

官方建議使用三元表達(dá)式來實(shí)現(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來實(shí)現(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)確實(shí)在變

于是我嘗試著打印currentComponent.value?.saveData方法,控制臺出現(xiàn)了。但是上層打印的確實(shí)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)文章

最新評論

武宣县| 灵山县| 兴文县| 十堰市| 南丹县| 珲春市| 聂荣县| 涞源县| 威海市| 榆中县| 湖州市| 南京市| 凤冈县| 社会| 古田县| 洛隆县| 芜湖县| 新干县| 仁化县| 鄯善县| 柯坪县| 改则县| 从江县| 延边| 中阳县| 清河县| 翁源县| 泗水县| 东光县| 巍山| 剑川县| 黄龙县| 闻喜县| 泸定县| 崇左市| 烟台市| 五常市| 灵山县| 乌拉特前旗| 清丰县| 泰宁县|