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 的不同标签来切换不同的路由和视图。

相关推荐
weixin_443353316 分钟前
小红书帖子评论的nodejs爬虫脚本
前端·爬虫
yzzzzzzzzzzzzzzzzz7 分钟前
HTML 常用标签介绍
前端·html
小奋斗10 分钟前
深入浅出:JavaScript中防抖与节流详解
javascript·面试
Wcy307651906610 分钟前
web前端第二次作业
前端·javascript·css
北京_宏哥10 分钟前
Python零基础从入门到精通详细教程11 - python数据类型之数字(Number)-浮点型(float)详解
前端·python·面试
waterHBO13 分钟前
css 模拟一个动画效果,消息堆叠。
前端·css
艾小码26 分钟前
JavaScript 排序完全指南:从基础到高阶实战
前端·javascript·排序算法
前端加油站29 分钟前
在 cursor 成为流行的开发方式后,作为普通开发我们能做什么
前端
Cache技术分享37 分钟前
163. Java Lambda 表达式 - Function 的链式组合
前端·后端
柯南95271 小时前
Vue 3 响应式系统源码解析
vue.js