浅析Vue3动态组件怎么进行异常处理

Vue3动态组件怎么进行异常处理?下面本篇文章带大家聊聊Vue3 动态组件异常处理的方法,希望对大家有所帮助!

 

动态组件有两种常用场景:

一是动态路由:

// 动态路由
export const asyncRouterMap: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'index',
    meta: { title: '首页' },
    component: BasicLayout, // 引用了 BasicLayout 组件
    redirect: '/welcome',
    children: [
      {
        path: 'welcome',
        name: 'Welcome',
        meta: { title: '引导页' },
        component: () => import('@/views/welcome.vue')
      },
      ...
    ]
  }
]

二是动态渲染组件,比如在 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>
      // 关键在这里
      <component :key="item.key || item.name" :is="item.component" v-bind="item.props" />
    </el-tab-pane>
  </template>
</el-tabs>

在 vue2 中使用并不会引发什么其他的问题,但是当你将组件包装成一个响应式对象时,在 vue3 中,会出现一个警告:

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.

出现这个警告是因为:使用 reactive 或 ref(在 data 函数中声明也是一样的)声明变量会做 proxy 代理,而我们组件代理之后并没有其他用处,为了节省性能开销,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)
}])

对于动态路由的处理:

import { markRaw } from 'vue'

// 动态路由
export const asyncRouterMap: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'home',
    meta: { title: '首页' },
    component: markRaw(BasicLayout), // 使用 markRaw
    // ...
  }
]

 而对于 shallowRef 和 markRaw,2 者的区别在于 shallowRef 只会对 value 的修改做出反应,比如:

const state = shallowRef({ count: 1 })
// 不会触发更改
state.value.count = 2
// 会触发更改
state.value = { count: 2 }

而 markRaw,是将一个对象标记为不可被转为代理。然后返回该对象本身。

const foo = markRaw({})
console.log(isReactive(reactive(foo))) // false
// 也适用于嵌套在其他响应性对象
const bar = reactive({ foo })
console.log(isReactive(bar.foo)) // false

可看到,被 markRaw 处理过的对象已经不是一个响应式对象了。

对于一个组件来说,它不应该是一个响应式对象,在处理时,shallowRef 和 markRaw 2 个 API,推荐使用 markRaw 进行处理。

以上就是浅析Vue3动态组件怎么进行异常处理的详细内容,有不当的地方还请大家多多指教

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

忧郁的蛋~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值