欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > elementplus的el-tabs路由式

elementplus的el-tabs路由式

2025/3/31 23:16:18 来源:https://blog.csdn.net/2301_76272808/article/details/146605145  浏览:    关键词:elementplus的el-tabs路由式

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

步骤 1: 安装必要的库

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

src/main.js:

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:

<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:

<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:

// 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 属性相匹配来实现路由跳转。

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词