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

相关推荐
北海-cherish5 小时前
vue中的 watchEffect、watchAsyncEffect、watchPostEffect的区别
前端·javascript·vue.js
AALoveTouch5 小时前
网球馆自动预约系统的反调试
javascript·网络
2501_915909066 小时前
HTML5 与 HTTPS,页面能力、必要性、常见问题与实战排查
前端·ios·小程序·https·uni-app·iphone·html5
white-persist7 小时前
Python实例方法与Python类的构造方法全解析
开发语言·前端·python·原型模式
新中地GIS开发老师7 小时前
Cesium 军事标绘入门:用 Cesium-Plot-JS 快速实现标绘功能
前端·javascript·arcgis·cesium·gis开发·地理信息科学
Superxpang7 小时前
前端性能优化
前端·javascript·vue.js·性能优化
左手吻左脸。7 小时前
解决el-select因为弹出层层级问题,不展示下拉选
javascript·vue.js·elementui
左手吻左脸。7 小时前
Element UI表格中根据数值动态设置字体颜色
vue.js·ui·elementui
李白的故乡7 小时前
el-tree-select名字
javascript·vue.js·ecmascript
Rysxt_7 小时前
Element Plus 入门教程:从零开始构建 Vue 3 界面
前端·javascript·vue.js