elementplus的el-tabs路由式

devtools/2025/3/31 22:52:58/

在使用 Element Plus 的 el-tabs 组件,实现路由式的切换(即点击标签页来切换不同的路由页面)。下面是一个基于 Vue 3 和 Element Plus 实现路由式 el-tabs 的基本步骤和示例。

步骤 1: 安装必要的库

在vue3项目安装 Vue Router 和 Element Plus。

src/main.js:

javascript">import { createApp } from 'vue';
import App from './components/el-tabs.vue';
import router from './router';
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';const app = createApp(App);
app.use(router);
app.use(ElementPlus);
app.mount('#app');

步骤 2: 设置 Vue Router

设置 Vue Router。例如,创建一个简单的路由配置,比如有两个页面:TabOne.vue 和 TabTwo.vue

src/components/TabOne.vue:

javascript"><template><div class="hello"><div style="color: red">这是配置管理子组件TabOne</div></div>
</template>
<style scoped >
.hello{width: 100%;height: 600px;display: flex;justify-content: center;align-items: center;background-color: #CAE1FF;
}
</style>

src/components/TabTwo.vue:

javascript"><template><div class="hello"><div style="color: red">这是配置管理子组件TabTwo</div></div>
</template><style scoped >
.hello{width: 100%;height: 600px;display: flex;justify-content: center;align-items: center;background-color: #CAE1FF;
}
</style>

src/components/el-tabs.vue:

javascript">// router/index.js
import { createRouter, createWebHistory } from 'vue-router';
import TabOne from '../components/TabOne.vue';
import TabTwo from '../components/TabTwo.vue';const routes = [{ path: '/tab-one', name: 'TabOne', component: TabOne },{ path: '/tab-two', name: 'Profile', component: TabTwo },];const router = createRouter({history: createWebHistory(),routes,
});export default router;

步骤 3: 使用 el-tabs 和 Vue Router

在 Vue 组件中使用 el-tabs,并通过监听 el-tab-pane 的 name 属性与 Vue Router 的 to 属性相匹配来实现路由跳转。

javascript"><template><el-tabs v-model="activeTab" @tab-click="handleTabClick"><el-tab-pane label="Tab 1" name="/tab-one"> </el-tab-pane> <!-- 注意这里使用的是路径 --><el-tab-pane label="Tab 2" name="/tab-two"> </el-tab-pane> <!-- 注意这里使用的是路径 --></el-tabs><router-view/> <!-- 使用 router-view 来显示当前路由对应的组件 -->
</template>
<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';const router = useRouter();const activeTab = ref('/tab-one'); // 默认激活的标签页const handleTabClick = (tab) => {router.push(tab.props.name); // 切换路由到对应的标签页路径}</script>

步骤 4: 运行

这样,就可以在 Element Plus 的 el-tabs 组件中实现一个路由式的标签页切换功能了。通过点击 el-tabs 的不同标签来切换不同的路由和视图。


http://www.ppmy.cn/devtools/172073.html

相关文章

【Elasticsearch基础】CRUD操作实践

Elasticsearch作为最流行的搜索和分析引擎&#xff0c;其核心CRUD&#xff08;创建、读取、更新、删除&#xff09;操作是每个开发者必须掌握的技能。本文将详细介绍Elasticsearch的基础数据操作&#xff0c;并提供可直接复用的curl示例。 1 创建索引与文档 1.1 创建索引 // …

程序员软件工具推荐列表

&#x1f91f;致敬读者 &#x1f7e9;感谢阅读&#x1f7e6;笑口常开&#x1f7ea;生日快乐⬛早点睡觉 &#x1f4d8;博主相关 &#x1f7e7;博主信息&#x1f7e8;博客首页&#x1f7eb;专栏推荐&#x1f7e5;活动信息 文章目录 程序员软件工具推荐列表1. Snipaste2. VSCod…

Python入门基础

python基础类型转换 str()与int()类型转换 name 张三 age 20 print(type(name),type(age))print(我叫name 今年&#xff0c; str(age)岁 )a10 b198.8 cFalse print(type(a),type(b),type(c)) print(str(a),str(b),str(c))s1 128 f198.7 s276.77 ffTrue s3hello print(type(s…

什么是索引下推和索引覆盖?

一、索引下推&#xff08;Index Condition Pushdown, ICP&#xff09; 1. 什么是索引下推&#xff1f; 核心思想&#xff1a;在存储引擎层&#xff08;如 InnoDB&#xff09;提前过滤数据&#xff0c;减少不必要的回表操作。通俗解释&#xff1a;假设你有一个联合索引&#x…

71. 我的第一个Linux驱动实验

一、字符设备驱动框架 字符设备驱动的编写主要就是驱动对应的open、close、read。。。其实就是 file_operations结构体的成员变量的实现。 其中关于 C 库以及如何通过系统调用“陷入” 到内核空间这个我们不用去管&#xff0c;我们重点关注的是应用程序和具体的驱动&#xff0…

求最大公约数与最小公倍数

求最大公约数&#xff08;GCD&#xff09;与最小公倍数&#xff08;LCM&#xff09; 1. 基本概念 GCD&#xff08;最大公约数&#xff09;&#xff1a;两个整数的最大公共因数LCM&#xff08;最小公倍数&#xff09;&#xff1a;两个整数的最小公共倍数数学关系&#xff1a;L…

微软下一个大更新:Windows 11 25H2或已在路上!

快科技3月26日消息&#xff0c;随着Windows 10支持即将在10月结束&#xff0c;越来越多的用户开始转向Windows 11 24H2&#xff0c;这也是目前强制性更新的最新版本。 不过Windows 11 24H2问题却一点不少&#xff0c;如蓝牙音频故障、文件资源管理器错误&#xff0c;甚至系统崩…

线程同步——读写锁

Linux——线程同步 读写锁 目录 一、基本概念 1.1 读写锁的基本概念 1.2 读写锁的优点 1.3 读写锁的实现 1.4 代码实现 一、基本概念 线程同步中的读写锁&#xff08;Read-Write Lock&#xff09;&#xff0c;也常被称为共享-独占锁&#xff08;Shared-Exclusive Lock&a…