美文网首页
【Vue3+ElementPlus】菜单路由功能比较

【Vue3+ElementPlus】菜单路由功能比较

作者: MasonChan | 来源:发表于2023-02-25 14:59 被阅读0次

方式一:通过动态组件的方式,监听浏览器 hashchange 事件或使用 History API 来更新当前组件

参考:https://cn.vuejs.org/guide/scaling-up/routing.html#official-router

<script setup>
import { ref, computed } from 'vue'
import Home from './Home.vue'
import About from './About.vue'
import NotFound from './NotFound.vue'

const routes = {
  '/': Home,
  '/about': About
}

const currentPath = ref(window.location.hash)

window.addEventListener('hashchange', () => {
  currentPath.value = window.location.hash
})

const currentView = computed(() => {
  return routes[currentPath.value.slice(1) || '/'] || NotFound
})
</script>

<template>
  <a href="#/">Home</a> |
  <a href="#/about">About</a> |
  <a href="#/non-existent-path">Broken Link</a>
  <component :is="currentView" />
</template>

优点:纯 Vue3 就可以实现,不需要引入额外的路由库
缺点:需要手撸事件监听代码,编译后体积较小

方式二:使用路由库 vue-router 进行路由

<script lang='ts' setup>

import { RouteLocationRaw, useRouter } from 'vue-router';

// 选择菜单时,在 <router-view> 渲染对应的组件
const router = useRouter()
function handleSelete(index: RouteLocationRaw) {
  router.push(index)
}

</script>

<template>
  <div class="common-layout">
    <el-container>
          <!-- Element Plus Menu: https://element-plus.gitee.io/zh-CN/component/menu.html -->
          <el-menu class="el-menu-demo" mode="horizontal" :default-active="$route.path" :ellipsis="false"
            @select="handleSelete">
            <el-menu-item index="/aichat">ChatGPT</el-menu-item>
            <el-menu-item index="/aitranslate">AI翻译</el-menu-item>
            <el-menu-item index="/aitext">AI文本</el-menu-item>
          </el-menu>
      <div style="height: 50px"></div>
      <router-view></router-view>
    </el-container>
  </div>
</template>

优点:路由功能强大,灵活性强
缺点:需要引入路由库 vue-router,编译后体积较大

方式三:使用 element plus 菜单的 router 进行路由

<script lang='ts' setup>

</script>

<template>
  <div class="common-layout">
    <el-container>
          <!-- Element Plus Menu: https://element-plus.gitee.io/zh-CN/component/menu.html -->
          <el-menu class="el-menu-demo" mode="horizontal" :default-active="$route.path" :ellipsis="false" router>
            <el-menu-item index="/aichat">ChatGPT</el-menu-item>
            <el-menu-item index="/aitranslate">AI翻译</el-menu-item>
            <el-menu-item index="/aitext">AI文本</el-menu-item>
          </el-menu>
      <div style="height: 50px"></div>
      <router-view></router-view>
    </el-container>
  </div>
</template>

router 属性启用后,可以省掉 handleSelete() 方法,就变成纯 element plus 路由了

优点:实现简单,纯 element plus 的 menu 组件就可以实现(当然底层渲染时还是会转换成 ts 代码的),不需要手撸任何逻辑
缺点:需要引入 UI 库 element plus,编译后体积较大

相关文章

  • UI组件模块设计思路

    模块设计思路 功能职责细分 UI组件 组件类型 如何创建组件 菜单获取 菜单缓存管理 菜单路由跳转 组件流式布局 ...

  • Vue3 + Element Plus 多级嵌套菜单动态渲染

    1 前言 1.1 功能 动态渲染多级嵌套菜单 点击菜单跳转页面 子菜单高亮,对应父级菜单也高亮 不同路由高亮同一菜...

  • The second day of the react proj

    注册路由 admin.js 后台管理的路由组件 导航菜单配置: config/menuConfig.js 导航菜单...

  • WORD 2016 文档比较功能

    WORD 2016 文档比较功能菜单入口:审阅→常用工具栏倒数第二组:比较|比较,分单纯比较及合并比较两个功能。 ...

  • Ant Design Pro Vue使用心得

    目录结构 路由和菜单 基本结构 路由和菜单是组织起一个应用的关键骨架,pro 中的路由为了方便管理,使用了中心化的...

  • Vue 默认展开路由所在的菜单

    默认展开路由所在的菜单

  • Sword框架

    一,配置路由 1,设置路由路径 2,设置菜单,按钮(本地) 3,设置菜单显示内容(国际化) 二,接口访问 1,设置...

  • 基于ant-pro脚手架的权限控制

    本周末花了两天对权限系统做了简单梳理,大致感悟如下: 权限系统的大致分类 菜单权限路由权限(ps:页面权限)功能权...

  • 路由和菜单

    路由和菜单是组织起一个应用的关键骨架,为了方便管理,使用了中心化的方式,在 router.config.js统一配...

  • 路由-菜单-权限

网友评论

      本文标题:【Vue3+ElementPlus】菜单路由功能比较

      本文链接:https://www.haomeiwen.com/subject/vgbfldtx.html