header监测路由变化

原文链接
在项目中通过命名视图在很多个页面行都设置了头部组件,在项目中就遇到了这么一个问题,这个头部只会加载一次,事实上虽然头部会公用,但是不是所有的头部都一样的,有些页面的头部需要做一些多余的功能,该怎么搞
在mounted中定义方法是没用的,因为头部只会加载一次,这时我们就可以通过监控路由,路由变化了,就会执行函数,就可以实现不同页面显示不同的头部了。

  watch: {
    '$route': 'judgeHelp'
  },
  methods: {
    // 判断help页面显示与否
    judgeHelp () {
      let router = this.$route.path
      console.log(router)
      // 获取到路由后可以在这里做相关判断
    },
  }

由此就可以实现不同页面的不同显示了。
这样还有一个问题,刷新页面,函数就不会执行了,因为路由没变化,这时我们在mounted中再执行一遍函数就可以了。

  mounted () {
    this.judgeHelp()
  }
vue左侧动态菜单栏实现<template> <div class="toubu"> <div class="header" v-if="this.caidanqiehuan == 'xian1'"> <keep-alive> <Map class="dzMap" v-if="$route.meta.showMap"> <div class="header-left2"> <nav> <div v-for="item in caidanlan" :key="item.id" :class=" $route.name === item.name ? 'left-beijing' : 'left-yanse' " @click="handleTabClick(item.id)" > <router-link :to="{ name: item.name }" class="kk"> <img class="header-image" :src="item.image" alt="" /> <p>{{ item.name }}</p> </router-link> </div> </nav> </div> <div class="header-right1"> <div class="right-content"> <router-view></router-view> </div> </div> </Map> <div class="dzMap1" v-else> <div class="header-left3"> <nav> <div v-for="item in caidanlan" :key="item.id" :class="ck === item.id ? 'left-beijing' : 'left-yanse'" @click="handleTabClick(item.id)" > <router-link :to="{ name: item.name }" class="kk"> <img class="header-image" :src="item.image" alt="" /> <p>{{ item.name }}</p> </router-link> </div> </nav> </div> <div class="header-right2"> <div class="right-content"> <router-view></router-view> </div> </div> </div> </keep-alive> </div> </div> </template> <script> import router from "@/router"; import { RouterLink, RouterView } from "vue-router"; import Map from "@/views/qxjcfwpt/Map.vue"; import MapView from "@/views/qxjcfwpt/dysjrh/components/MapView.vue"; export default { name: "qxsjcxtj-page", components: { Map, MapView }, data() { return { caidanqiehuan: "xian1", tabPosition: "left", ck: 1, e: 1, routes: ["地灾预警"], activeIndex: "", isActive: "", caidanlan: [], //地图类数据 overViewLayer: [], Map: null, dataMapLayer: {}, //梦图矢量地图服务 // 1、监听当前视口的范围 CurMapExtent: [], //;墨卡托范围 MapServiceLayers: [], //复位数据 initCenter: "", defaultProps: { children: "children", label: "label", }, menuDataParsed: null, // 缓存解析后的菜单数据 }; }, mounted() { this.handleTabClick(this.e); this.caidan(); }, methods: { // 动态菜单 caidan() { if (!this.menuDataParsed) { const menuData = localStorage.getItem("menu"); if (menuData) { this.menuDataParsed = JSON.parse(menuData); } else { console.error("Menu data not found in localStorage"); return; } } const parsedData = this.menuDataParsed; let children1 = parsedData[0].children.find((item) => { return ( item.name === "实况信息" && Array.isArray(item.children) && item.children.length > 0 ); })?.children || []; this.caidanlan = children1.map((item) => ({ ...item, image: require(`@/assets/svg/${item.name}.png`), // 动态处理图片路径 })); if (!this.caidanlan.length) return; const first = this.caidanlan[0].name; this.routes = [first]; this.activeIndex = first; this.ck = this.caidanlan[0].id; console.log(first, "66"); /* 只在当前路由就是 /hy 时才重定向 */ if (this.$route.path === "/skxx" || this.$route.name === "实况信息") { this.$router.replace({ name: first }); } // } }, handleTabClick(e) { console.log(e, "77"); this.ck = e; // 获取当前路由 const currentRoute = this.$route; // 获取当前路由的名称 const currentRouteName = currentRoute.name; // 获取当前路由的完整路径 const currentRoutePath = currentRoute.path; }, }, watch: { $route(newRoute) { console.log(newRoute, "777"); // 检查数组中是否已经包含当前路由的名称 if (!this.routes.includes(newRoute.name)) { this.routes.push(newRoute.name); } this.activeIndex = newRoute.name; }, }, }; </script> <style lang="less" scoped> .header-left2::-webkit-scrollbar { display: none; /* 隐藏滚动条 */ } .header-left3::-webkit-scrollbar { display: none; /* 隐藏滚动条 */ } .header { width: 100vw; height: 91vh; display: flex; // border: 1px solid red; overflow: hidden; position: relative; .header-right { flex: 1; // border: 1px solid black; background-color: #eee; .right-botton { width: 100%; height: 2.5rem; display: flex; align-items: center; flex-direction: row; // border: 1px solid black; background-color: #b8daf6; .kkk { width: 7rem; height: 2rem; // border: 1px solid black; color: #297dd3; border: 1px solid #297dd3; font-size: 0.875rem; display: flex; align-items: center; justify-content: center; background-color: #e5f1ff; margin-left: 0.9375rem; border-radius: 0.3125rem; // font-weight: bold; font-family: "思源黑体"; .anniu { margin-left: 0.375rem; font-size: 0.75rem; } } .kkk1 { width: 7rem; height: 2rem; color: #297dd3; font-size: 0.875rem; display: flex; align-items: center; justify-content: center; background-color: #92c0ec; border: 1px solid #297dd3; margin-left: 0.9375rem; border-radius: 0.3125rem; // font-weight: bold; font-family: "思源黑体"; .anniu { margin-left: 0.375rem; font-size: 0.75rem; } } } .right-content { width: 100%; height: 95%; background-color: #eee; position: relative; // border: 1px solid black; } } .header-right1 { // border: 1px solid black; background-color: #eee; position: relative; pointer-events: auto; .right-content { width: 100%; height: 100%; background-color: #fff; // border: 1px solid black; } } .header-right2 { flex: 1; // border: 1px solid black; background-color: #eee; .right-content { width: 100%; height: 100%; background-color: #fff; // border: 1px solid black; } } .header-left { width: 15rem; height: 100%; // border: 1px solid black; display: flex; flex-direction: column; align-items: center; background-color: #1f75cd; .header-image { width: 1.25rem; margin-right: 0.75rem; margin-left: 2.25rem; margin-top: 0.125rem; } .left-yanse { width: 15rem; height: 3.75rem; display: flex; align-items: center; justify-content: center; } .kk { width: 15rem; height: 3.75rem; display: flex; align-items: center; p { font-size: 1rem; } } .left-yanse p { color: white; } .left-beijing { width: 15rem; height: 3.75rem; display: flex; align-items: center; justify-content: center; background: url(@/assets/image/15.png); background-size: 100% 100%; background-position: center; background-repeat: no-repeat; } .left-beijing p { color: white; } } .header-left1 { width: 4rem; height: 100%; // border: 1px solid black; display: flex; flex-direction: column; align-items: center; background-color: #1f75cd; .header-image { width: 1.25rem; } .left-yanse { width: 4rem; height: 3.75rem; // background-color: #2c72ad; display: flex; align-items: center; justify-content: center; } .kk { width: 4rem; height: 3.75rem; display: flex; align-items: center; justify-content: center; p { font-size: 1rem; } } .left-yanse p { color: white; } .left-beijing { width: 4rem; height: 3.75rem; display: flex; align-items: center; justify-content: center; background: url(@/assets/image/15.png); background-size: 100% 100%; background-position: center; background-repeat: no-repeat; } .left-beijing p { color: white; } } .header-left2 { width: 6rem; height: 100%; // border: 1px solid black; // background-color: #eee; display: flex; flex-direction: column; align-items: center; // padding-top: 0.625rem; // background-color:#eee; overflow: auto; pointer-events: auto; // position: absolute; // top:0; // left:-100rem; .header-image { width: 2rem; display: flex; align-items: center; justify-content: center; // border: 1px solid black; } .left-yanse { width: 5rem; height: 4rem; display: flex; align-items: center; justify-content: center; background-color: #1f75cd; // border: 1px solid black; margin-top: 0.375rem; border-radius: 0.375rem; } .left-yanse p { color: white; } .kk { width: 15rem; height: 3.75rem; display: flex; flex-direction: column; align-items: center; justify-content: center; p { font-size: 0.8rem; } } .left-beijing { width: 5rem; height: 4rem; display: flex; align-items: center; justify-content: center; border-radius: 0.375rem; margin-top: 0.375rem; background-color: #f59a23; } .left-beijing p { color: white; } } .header-left3 { width: 6rem; height: 100%; // border: 1px solid black; // background-color: #eee; display: flex; flex-direction: column; align-items: center; // padding-top: 0.625rem; // background-color:#eee; overflow: auto; .header-image { width: 2rem; display: flex; align-items: center; justify-content: center; // border: 1px solid black; } .left-yanse { width: 5rem; height: 4rem; display: flex; align-items: center; justify-content: center; background-color: #1f75cd; // border: 1px solid black; margin-top: 0.375rem; border-radius: 0.375rem; } .left-yanse p { color: white; } .kk { width: 15rem; height: 3.75rem; display: flex; flex-direction: column; align-items: center; justify-content: center; p { font-size: 0.8rem; } } .left-beijing { width: 5rem; height: 4rem; display: flex; align-items: center; justify-content: center; border-radius: 0.375rem; margin-top: 0.375rem; background-color: #f59a23; } .left-beijing p { color: white; } } .dzMap { width: 100%; height: 100%; // border: 1px solid red; display: flex; position: absolute; } .dzMap1 { width: 100%; height: 100%; // border: 1px solid red; position: absolute; display: flex; } } </style> 我的代码再点击左侧菜单时,右边的页面反应过慢,过了一会才跳转过来怎么优化这个问题
10-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值