elementplus的el-tabs路由式

在使用 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.vueTabTwo.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-panename 属性与 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 的不同标签来切换不同的路由和视图。

相关推荐
前端小趴菜0523 分钟前
React - 组件通信
前端·react.js·前端框架
Amy_cx43 分钟前
在表单输入框按回车页面刷新的问题
前端·elementui
dancing9991 小时前
cocos3.X的oops框架oops-plugin-excel-to-json改进兼容多表单导出功能
前端·javascript·typescript·游戏程序
后海 0_o1 小时前
2025前端微服务 - 无界 的实战应用
前端·微服务·架构
Scabbards_1 小时前
CPT304-2425-S2-Software Engineering II
前端
小满zs2 小时前
Zustand 第二章(状态处理)
前端·react.js
程序猿小D2 小时前
第16节 Node.js 文件系统
linux·服务器·前端·node.js·编辑器·vim
萌萌哒草头将军2 小时前
🚀🚀🚀Prisma 发布无 Rust 引擎预览版,安装和使用更轻量;支持任何 ORM 连接引擎;支持自动备份...
前端·javascript·vue.js
狼性书生2 小时前
uniapp实现的简约美观的星级评分组件
前端·uni-app·vue·组件
书语时2 小时前
ES6 Promise 状态机
前端·javascript·es6