学习使用vue-router tab切换(三)
页签规则
页签规则定义了路由打开页签的方式。
通过配置路由的 meta.key
属性,您可以定制路由页签规则
默认规则
默认情况下,同一路由打开同一个页签
内置规则
path
- 规则:
route => route.path
- 说明:相同
route.params
的路由,route.path
一致,共用页签
示例:
const route = { path: '/my-page/:id', component: MyPage, meta: { title: '页面', key: 'path' } }
根据示例中的页签规则:
/my-page/1
和/my-page/2
的params
参数不同,会打开独立的页签/my-page/1
、/my-page/1?a=1
和/my-page/1?b=2
的params
参数相同,会共用同一个页签
fullPath
- 规则:
route => route.fullPath.replace(route.hash, '')
- 说明:相同
route.params
和route.query
的路由,route.fullPath
去除hash
后一致,共用页签
示例:
const route = { path: '/my-page/:id', component: MyPage, meta: { title: '页面', key: 'fullPath' } }
自定义规则
除了使用内置规则,你还可以用函数自定义控制
示例:
const route = { path: '/my-page/:catalog/:type', component: MyPage, meta: { title: '定制规则', key(route) { return `/my-page/${route.params.catalog}` } } }
根据示例中的页签规则:
/my-page/a/1
和/my-page/a/2
,params.catalog
相同,打开的是同一个页签/my-page/a/1
和/my-page/b/1
,params.catalog
不同,则打开独立的页签
Iframe 页签
outerTab 支持通过 Iframe 页签嵌入外部网站。
注意
该功能需要引入 RouterTab 内置路由,请参考 基础 - 路由配置
Iframe 页签操作
打开 Iframe 页签
// 三个参数分别为:链接、页签标题、图标 this.$tabs.openIframe('https://cn.vuejs.org', 'Vue.js', 'icon-web')
关闭 Iframe 页签
this.$tabs.closeIframe('https://cn.vuejs.org')
刷新 Iframe 页签
this.$tabs.refreshIframe('https://cn.vuejs.org')
Iframe 页签事件
RouterTab 支持以下的 Iframe 页签事件:
-
iframe-mounted
iframe 节点挂载就绪 -
iframe-loaded
iframe 内容加载成功
需要注意的是,iframe 内部链接跳转也会触发 iframe-loaded
事件
示例:
<template> <router-tab @iframe-mounted="iframeMounted" @iframe-loaded="iframeLoaded" /> </template> <script> export default { methods: { // iframe 节点挂载就绪 iframeMounted(url, iframe) { console.log('iframe-mounted:', url, iframe.contentWindow) }, // iframe 内容加载成功 iframeLoaded(url, iframe) { console.log('iframe-loaded:', url, iframe.contentWindow) } } } </script>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!