溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Vue3動態(tài)組件如何進(jìn)行異常處理

發(fā)布時(shí)間:2022-12-03 09:12:59 來源:億速云 閱讀:182 作者:iii 欄目:編程語言

這篇“Vue3動態(tài)組件如何進(jìn)行異常處理”文章的知識點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Vue3動態(tài)組件如何進(jìn)行異常處理”文章吧。

動態(tài)組件有兩種常用場景:

一是動態(tài)路由:

// 動態(tài)路由
export const asyncRouterMap: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'index',
    meta: { title: '首頁' },
    component: BasicLayout, // 引用了 BasicLayout 組件
    redirect: '/welcome',
    children: [
      {
        path: 'welcome',
        name: 'Welcome',
        meta: { title: '引導(dǎo)頁' },
        component: () => import('@/views/welcome.vue')
      },
      ...
    ]
  }
]

二是動態(tài)渲染組件,比如在 Tabs 中切換:

    <el-tabs :model-value="copyTabName" type="card">
      <template v-for="item in tabList" :key="item.key || item.name">
        <el-tab-pane
          :name="item.key"
          :label="item.name"
          :disabled="item.disabled"
          :lazy="item.lazy || true"
        >
          <template #label>
            <span>
              <component v-if="item.icon" :is="item.icon" />
              {{ item.name }}
            </span>
          </template>
          // 關(guān)鍵在這里
          <component :key="item.key || item.name" :is="item.component" v-bind="item.props" />
        </el-tab-pane>
      </template>
    </el-tabs>

在 vue2 中使用并不會引發(fā)什么其他的問題,但是當(dāng)你將組件包裝成一個(gè)響應(yīng)式對象時(shí),在 vue3 中,會出現(xiàn)一個(gè)警告:

Vue received a Component which 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.

出現(xiàn)這個(gè)警告是因?yàn)椋?strong>使用 reactive 或 ref(在 data 函數(shù)中聲明也是一樣的)聲明變量會做 proxy 代理,而我們組件代理之后并沒有其他用處,為了節(jié)省性能開銷,vue 推薦我們使用 shallowRef 或者 markRaw 跳過 proxy 代理。

解決方法如上所說,需要使用 shallowRef 或 markRaw 進(jìn)行處理:

對于 Tabs 的處理:

import { markRaw, ref } from 'vue'

import A from './components/A.vue'
import B from './components/B.vue'

interface ComponentList {
  name: string
  component: Component
  // ...
}

const tab = ref<ComponentList[]>([{
    name: "組件 A",
    component: markRaw(A)
}, {
    name: "組件 B",
    component: markRaw(B)
}])

對于動態(tài)路由的處理:

import { markRaw } from 'vue'

// 動態(tài)路由
export const asyncRouterMap: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'home',
    meta: { title: '首頁' },
    component: markRaw(BasicLayout), // 使用 markRaw
    // ...
  }
]

而對于 shallowRef 和 markRaw,2 者的區(qū)別在于 shallowRef 只會對 value 的修改做出反應(yīng),比如:

const state = shallowRef({ count: 1 })

// 不會觸發(fā)更改
state.value.count = 2

// 會觸發(fā)更改
state.value = { count: 2 }

而 markRaw,是將一個(gè)對象標(biāo)記為不可被轉(zhuǎn)為代理。然后返回該對象本身。

const foo = markRaw({})
console.log(isReactive(reactive(foo))) // false

// 也適用于嵌套在其他響應(yīng)性對象
const bar = reactive({ foo })
console.log(isReactive(bar.foo)) // false

可看到,被 markRaw 處理過的對象已經(jīng)不是一個(gè)響應(yīng)式對象了。

對于一個(gè)組件來說,它不應(yīng)該是一個(gè)響應(yīng)式對象,在處理時(shí),shallowRef 和 markRaw 2 個(gè) API,推薦使用 markRaw 進(jìn)行處理。

以上就是關(guān)于“Vue3動態(tài)組件如何進(jìn)行異常處理”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI