【vue】导航栏变动后刷新router的几种方法

方法1:window.location.reload()

强制整页刷新

这种方法最为直接,但会中断当前的用户体验,有瞬间白屏。

方法2:使用hidden控制menu渲染

1、适合APP这种通过输入网址进入界面情况;

2、menu一般在login进入主页面后只加载一次不会频繁触发,所以onMounted中可以执行菜单显隐控制逻辑。

router/index.js

javascript 复制代码
import { createRouter, createWebHistory } from 'vue-router';
import Layout from '@/layout/index.vue';
import { useAppStore } from '../store';
import { invoke } from '@tauri-apps/api/core';

const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_BASE_URL),
  routes: [
    {
      path: '/account/login',
      name: 'login',
      hidden: true,
      component: () => import('@/views/account/login.vue'),
    },
    {
      path: '/',
      component: Layout,
      redirect: '/account/login',
      children: [
        {
          path: 'dashboard',
          name: 'Home',
          component: () => import('@/views/entry/index.vue'),
          meta: {
            title: 'menu.home',
            icon: 'menu_home',
            affix: true,
          },
        },
      ],
    },
    {
      path: '/robot',
      component: Layout,
      children: [
        {
          path: 'robot',
          name: 'RobotManage', // 没有name面包屑不展示
          component: () => import('@/views/robot/index.vue'),
          meta: {
            title: 'menu.robot', // 各处导航标题
            icon: 'menu_robot',
          },
        },
      ],
    },
    {
      path: '/setting',
      component: Layout,
      redirect: '/setting/scene',
      children: [
        {
          path: 'scene',
          name: 'SceneManage',
          component: () => import('@/views/setting/scene/sceneManage/index.vue'),
          meta: {
            title: 'menu.config',
            icon: 'menu_setting',
          },
        },
      ],
    },
    {
      path: '/log',
      component: Layout,
      children: [
        {
          path: 'log',
          name: 'LogManage',
          component: () => import('@/views/log/index.vue'),
          meta: {
            title: 'menu.logs',
            icon: 'menu_log',
          },
        },
      ],
    },
    {
      path: '/map',
      component: Layout,
      children: [
        {
          path: 'map',
          name: 'MapManage',
          component: () => import('@/views/map/index.vue'),
          meta: {
            title: 'menu.design',
            icon: 'menu_map',
          },
        },
      ],
    },
    {
      path: '/404',
      component: () => import('@/views/404.vue'),
      hidden: true,
    },
    {
      path: '/:pathMatch(.*)*',
      redirect: '/404',
      hidden: true,
    },
  ],
});

export const resetRouter = () => {
  // console.log(router)
};

/**
 * 路由拦截设置
 *
 */
router.beforeEach(async (to, from, next) => {
  console.log('监测路由变化:从', from.path, '到', to.path);
  next();
  return;
});

export default router;

menu导航页面:

html 复制代码
<template>
  <el-scrollbar :height="props.height" id="router-menu">
    <!-- 必须添加background-color属性,因为收起菜单时 悬浮二级菜单默认为白色背景 需要重置 -->
    <el-menu
      :active-text-color="variables.menuActiveText"
      :background-color="variables.menuBg"
      :collapse="isCollapse"
      :collapse-transition="false"
      :default-active="activeMenu"
      :text-color="variables.menuText"
      :unique-opened="false"
      mode="vertical"
    >
      <item :base-path="route.path" :info="route" :key="route.path" v-for="route in routes" />
    </el-menu>
  </el-scrollbar>
</template>

<script setup>
import { onMounted, onUnmounted, ref, computed, inject } from 'vue';
import { useRouter, useRoute } from 'vue-router';
// import { onBeforeRouteUpdate } from 'vue-router'

import variables from '@/styles/variables.module.scss';
import Item from './Item.vue';

const props = defineProps({
  height: {
    type: String,
    default: '100%',
  },
  isCollapse: {
    type: Boolean,
    default: false,
  },
});

const routes = ref(useRouter().options.routes);
const route = useRoute();

const activeMenu = computed(() => {
  const { meta, path } = route;
  // if set path, the sidebar will highlight the path you set
  if (meta.activeMenu) {
    return meta.activeMenu;
  }
  return path;
});

const updateMenuRoutes = () => {
  routes.value.forEach(item => {
    if (item.path === '/robot' && localStorage.getItem('features') === 'emulator') {
      item.hidden = true;
    } else if (item.path === '/robot' && localStorage.getItem('features') !== 'emulator') {
      item.hidden = false;
    }
  });
};

onMounted(() => {
  updateMenuRoutes();
});
</script>
相关推荐
衡石科技6 小时前
一个门户只保留一个智能助理:衡石嵌入式 Data Agent 的运行与渲染边界
前端·javascript·html·data agent
Hilaku6 小时前
一个前端工程师除了写代码,还能靠什么赚钱?
前端·javascript·程序员
我先测了6 小时前
Midscene.js 全栈学习指南:从入门到精通的 AI 视觉自动化框架
javascript·人工智能·自动化·agent·midscene
程序员Sunday6 小时前
JavaScript 闭包是什么?为什么变量没有被释放,什么时候会内存泄漏?
前端·javascript
JarvanMo6 小时前
Flutter 终于有自己的 React Flow 了,vyuh_node_flow 上手实录
前端
狂师6 小时前
爆火!新一代e2e AI测试框架,单日狂揽1500 star
前端·人工智能·测试
恋猫de小郭6 小时前
Flitz ,一个把 Flutter 玩成热插拔 Preview 的有趣项目,不错的思路
android·前端·flutter
闭包不眠6 小时前
Word转HTML后体积不对,裁掉的截图原图还在
前端·图像处理·人工智能·计算机视觉·html·word
言乐66 小时前
PythonLOLM自动生成复盘报告模型
开发语言·前端·数据库·人工智能·python
tryCbest6 小时前
第二篇:CSS3 全部知识点全解 + 项目样式实现
前端·css·css3