欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

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

10余年的新干網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都全網(wǎng)營銷的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整新干建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)建站從事“新干網(wǎng)站設(shè)計”,“新干網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實執(zhí)行。

動態(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)你將組件包裝成一個響應(yīng)式對象時,在 vue3 中,會出現(xiàn)一個警告:

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)這個警告是因為:使用 reactive 或 ref(在 data 函數(shù)中聲明也是一樣的)聲明變量會做 proxy 代理,而我們組件代理之后并沒有其他用處,為了節(jié)省性能開銷,vue 推薦我們使用 shallowRef 或者 markRaw 跳過 proxy 代理。

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

對于 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,是將一個對象標(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)不是一個響應(yīng)式對象了。

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

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

分享標(biāo)題:Vue3動態(tài)組件如何進行異常處理
本文地址:http://www.chinadenli.net/article30/gisgpo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器企業(yè)網(wǎng)站制作商城網(wǎng)站定制網(wǎng)站網(wǎng)站導(dǎo)航網(wǎng)頁設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計