网站首页 > 精选文章 正文
动态组件有两种常用场景:
一是动态路由:
// 动态路由
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: 'index',
meta: { title: '首页' },
component: markRaw(BasicLayout), // 使用 markRaw
redirect: '/welcome',
// ...
}
]
而对于 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 推荐在使用动态组件用 markRaw 进行处理。
- 上一篇: Vue3.0权限管理实现流程【实践】
- 下一篇: React 对比 Vue 如何做路由鉴权?
猜你喜欢
- 2024-12-29 前端vue与后端Thinkphp在服务器的部署
- 2024-12-29 第03节:创建登陆页
- 2024-12-29 详解Nginx代理Vue3项目的实践与配置
- 2024-12-29 React/Vue路由全攻略:鉴权、导航守卫与拦截,让你站在技术之巅
- 2024-12-29 Vue3.2项目架构详解
- 2024-12-29 vue项目本地开发完成后部署到服务器后报404是什么原因呢?
- 2024-12-29 Vue路由配置方法详细介绍
- 2024-12-29 vue 路由守卫
- 2024-12-29 React 对比 Vue 如何做路由鉴权?
- 2024-12-29 Vue3.0权限管理实现流程【实践】
- 最近发表
- 标签列表
-
- 向日葵无法连接服务器 (32)
- git.exe (33)
- vscode更新 (34)
- dev c (33)
- git ignore命令 (32)
- gitlab提交代码步骤 (37)
- java update (36)
- vue debug (34)
- vue blur (32)
- vscode导入vue项目 (33)
- vue chart (32)
- vue cms (32)
- 大雅数据库 (34)
- 技术迭代 (37)
- 同一局域网 (33)
- github拒绝连接 (33)
- vscode php插件 (32)
- vue注释快捷键 (32)
- linux ssr (33)
- 微端服务器 (35)
- 导航猫 (32)
- 获取当前时间年月日 (33)
- stp软件 (33)
- http下载文件 (33)
- linux bt下载 (33)