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

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

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

1.動(dòng)態(tài)組件綁定問(wèn)題

上周用vue3的動(dòng)態(tài)組件替換了v-fo循環(huán)的頁(yè)面,在實(shí)際中遇到了一個(gè)頭疼的問(wèn)題。組件單獨(dú)寫是可以出來(lái),但使用動(dòng)態(tài)組件卻提示組件未注冊(cè)

組件未注冊(cè)

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

報(bào)錯(cuò)信息

vue 3 Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the "name" option.

我以為是setup 寫法 name丟失的問(wèn)題

在百度上看到最多的還是將“name”屬性默認(rèn)導(dǎo)出,嘗試了也無(wú)濟(jì)于事。

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

官方建議使用三元表達(dá)式來(lái)實(shí)現(xiàn)動(dòng)態(tài)組件:is="someCondition ? Foo : Bar"

解決方案

更具這個(gè)思路,那就很好處理了,只要將currentMode的值設(shè)置為elevator、airCond不就好了,搞個(gè)鍵值對(duì)映射就可以搞定了

function getComponent() {
  const component = {
    Plan1: Plan1,
    Plan2: Plan2,
    Plan3: Plan3,
  };
  return component[loadCountFlag.value];
}

最后將 getComponent()賦值給動(dòng)態(tài)組件,刷新頁(yè)面查看控制臺(tái),頁(yè)面無(wú)報(bào)錯(cuò)

如果你的動(dòng)態(tài)組件過(guò)多,可以使用defineAsyncComponent來(lái)實(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.動(dòng)態(tài)組件的方法導(dǎo)出問(wèn)題

動(dòng)態(tài)組件解決了,之前每個(gè)組件還有一個(gè)數(shù)據(jù)保存的方法,需要在父頁(yè)面自動(dòng)調(diào)用,并暴漏出去

于是這就很簡(jiǎn)單嗎,直接使用vue2中this.$refs.xxx.xxx不就好了

正當(dāng)我高高興興提交完代碼準(zhǔn)備下班時(shí),測(cè)試的同事把我叫住了說(shuō)自動(dòng)保存沒(méi)觸發(fā),數(shù)據(jù)丟失了

問(wèn)了了GPT:

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

于是我嘗試著打印currentComponent.value?.saveData方法,控制臺(tái)出現(xiàn)了。但是上層打印的確實(shí)undefind

組件切換的時(shí)候,autoSave應(yīng)該動(dòng)態(tài)切換,autoSave應(yīng)該時(shí)響應(yīng)式的,那么問(wèn)題就解決了

解決辦法:

使用computed包裹一層

問(wèn)題解決,下班!

到此這篇關(guān)于vue3中動(dòng)態(tài)組件的踩坑記錄分享的文章就介紹到這了,更多相關(guān)vue3動(dòng)態(tài)組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

都匀市| 手游| 安阳市| 博爱县| 古浪县| 秦安县| 寻甸| 合山市| 长武县| 宜州市| 五河县| 常熟市| 佛山市| 额尔古纳市| 富源县| 乌鲁木齐市| 康保县| 黄山市| 建德市| 灵武市| 麟游县| 古浪县| 西乡县| 涟源市| 绥滨县| 通州区| 涿鹿县| 建德市| 赣榆县| 陇西县| 文化| 扶沟县| 屯门区| 翼城县| 康保县| 韶山市| 湘潭市| 武夷山市| 沧源| 田阳县| 南京市|