How to handle exceptions in Vue3 dynamic components? The following article will talk about Vue3 dynamic component exception handling methods. I hope it will be helpful to everyone!
[Related recommendations: vuejs video tutorial]
There are two common scenarios for dynamic components:
The first is dynamic routing:
// 动态路由 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') }, ... ] } ]
The second is dynamic rendering components, such as switching in 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>
will not cause any other problems when used in vue2, but when you wrap the component into a responsive object, in vue3, a warning will appear:
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
.
This warning appears because: Using reactive or ref (the same is true for declaration in the data function), declaring variables will act as a proxy, and our component has no other use after proxying , in order to save performance overhead, vue recommends that we use shallowRef or markRaw to skip the proxy proxy.
The solution is as mentioned above, you need to use shallowRef or markRaw for processing:
For Tabs processing:
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) }])
For dynamic routing processing:
import { markRaw } from 'vue' // 动态路由 export const asyncRouterMap: Array<RouteRecordRaw> = [ { path: '/', name: 'home', meta: { title: '首页' }, component: markRaw(BasicLayout), // 使用 markRaw // ... } ]
The difference between shallowRef and markRaw is that shallowRef will only respond to value modifications, such as:
const state = shallowRef({ count: 1 }) // 不会触发更改 state.value.count = 2 // 会触发更改 state.value = { count: 2 }
And markRaw marks an object as not being converted to a proxy. The object itself is then returned.
const foo = markRaw({}) console.log(isReactive(reactive(foo))) // false // 也适用于嵌套在其他响应性对象 const bar = reactive({ foo }) console.log(isReactive(bar.foo)) // false
It can be seen that the object processed by markRaw is no longer a responsive object.
For a component, it should not be a responsive object. When processing, there are two APIs, shallowRef and markRaw. It is recommended to use markRaw for processing.
(Learning video sharing: web front-end development, Basic programming video)
The above is the detailed content of A brief analysis of how to handle exceptions in Vue3 dynamic components. For more information, please follow other related articles on the PHP Chinese website!

JavaScript 不提供任何内存管理操作。相反,内存由 JavaScript VM 通过内存回收过程管理,该过程称为垃圾收集。

vscode自身是支持vue文件组件跳转到定义的,但是支持的力度是非常弱的。我们在vue-cli的配置的下,可以写很多灵活的用法,这样可以提升我们的生产效率。但是正是这些灵活的写法,导致了vscode自身提供的功能无法支持跳转到文件定义。为了兼容这些灵活的写法,提高工作效率,所以写了一个vscode支持vue文件跳转到定义的插件。

Node 19已正式发布,下面本篇文章就来带大家详解了解一下Node.js 19的 6 大特性,希望对大家有所帮助!

选择一个Node的Docker镜像看起来像是一件小事,但是镜像的大小和潜在漏洞可能会对你的CI/CD流程和安全造成重大的影响。那我们如何选择一个最好Node.js Docker镜像呢?

本篇文章给大家整理和分享几个前端文件处理相关的实用工具库,共分成6大类一一介绍给大家,希望对大家有所帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools
