Vue页面手动刷新,实现导航栏激活项还原到初始状态
作者:Lena_叶 发布时间:2024-05-03 15:11:44
标签:Vue,导航栏,激活项,初始
场景描述:在页面中存在顶部导航和左侧导航,左侧导航和右侧内容区使用了命名视图实现,点击左侧导航的链接时,右侧内容区相应显示不同组件内容。问题:在当前链接手动刷新浏览器(例如:浏览器地址为/enterprise/list),顶部导航激活项还原到初始状态(这里默认是“工作台”项)。
原理:每次刷新都会重新实例化Vue,也就是会调用created方法。
<template>
<el-menu :default-active="defaultActiveIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect" :router="true">
<el-menu-item index="/">工作台</el-menu-item>
<el-menu-item index="/enterpriseManager">企业管理</el-menu-item>
<el-menu-item index="/orderManager">订单管理</el-menu-item>
<el-menu-item index="/systemManager">系统管理</el-menu-item>
</el-menu>
</template>
<script>
export default {
name: 'app',
data () {
return {
defaultActiveIndex: "/"
}
},
created() {
// 组件创建完后获取数据,
// 此时 data 已经被 observed 了
this.fetchData()
},
methods: {
handleSelect(index){
this.defaultActiveIndex = index;
},
jumpTo(url){
this.defaultActiveIndex = url;
this.$router.push(url); //用go刷新
},
fetchData () {
var cur_path = this.$route.path; //获取当前路由
var routers = this.$router.options.routes; // 获取路由对象
var nav_type = "";
for(var i=0; i<routers.length; i++){
var children = routers[i].children;
if(children){
for(var j=0; j<children.length; j++){
var grand_children = children[j].children;
if(grand_children){
for(var k=0; k<grand_children.length; k++){
if(grand_children[k].path == cur_path){
nav_type = routers[i].type;
break;
}
}
}
}
}
}
if(nav_type == "home"){
this.defaultActiveIndex = "/";
} else if(nav_type == "enterprise"){
this.defaultActiveIndex = "/enterpriseManager";
}
}
}
watch: {
'$route': 'fetchData'
}
}
</script>
附上router配置格式:
export default [
{
path: '/',
type: 'home', //自定义type区分不同模块菜单
name: 'home',
redirect: '/dashboard',
component: Home,
menuShow: true,
children: [
{
path: '/dashboard',
component: HomeNav,
name: 'dashboard',
leaf: true, // 只有一个节点
iconCls: 'icon-home', // 图标样式class
menuShow: true,
children: [
{ path: '/dashboard', component: Dashboard, name: '首页', menuShow: true }
]
},
{
path: '/mySet',
component: HomeNav,
name: '我的设置',
iconCls: 'el-icon-menu',
menuShow: true,
children: [
{ path: '/mySet/plan', component: Plan, name: '行程计划', menuShow: true },
{ path: '/mySet/maillist', component: Maillist, name: '通讯录', menuShow: true }
]
}
]
},
{
path: '/enterpriseManager',
type: 'enterprise',
name: 'enterprise',
component: Home,
redirect: '/enterprise/list',
menuShow: true,
children: [
{
path: '/enterpriseList',
component: EnterpriseNav,
name: 'enterpriseList',
leaf: true, // 只有一个节点
iconCls: 'icon-home', // 图标样式class
menuShow: true,
children: [
{ path: '/enterprise/list', component: EnterpriseList, name: '企业列表', menuShow: true }
]
},
{
path: '/enterpriseAdd',
component: EnterpriseNav,
name: 'enterpriseAdd',
leaf: true, // 只有一个节点
iconCls: 'el-icon-menu',
menuShow: true,
children: [
{ path: '/enterprise/add', component: EnterpriseAdd, name: '企业添加', menuShow: true }
]
},
{
path: '/enterpriseValidate',
component: EnterpriseNav,
name: 'enterpriseValidate',
leaf: true, // 只有一个节点
iconCls: 'el-icon-menu',
menuShow: true,
children: [
{ path: '/enterprise/validate', component: EnterpriseValidate, name: '企业认证', menuShow: true }
]
}
]
}
]
补充知识:vue手动刷新视图以及其他小问题
最近把手头上一个使用angularJS+jquery各种七七八八组件写的页面拿vue+elementUI重写了一边, 真是极度丝滑, 记录一些vue和elementUI的小问题
1.如果vue中的数据结构比较庞大的话, 十分有可能会出现model更新而视图不更新/model和视图都不更新也不报错的情况, 此时需要手动刷新vue的数据, 在change或click事件中, 使用this.$forceUpdate()手动刷新视图
//像这样
changeSef: function () {
//手动刷新视图
var that = this;
that.$forceUpdate();
},
2.在vue中使用setTimeout
//错误示范
setTimeout(bidOrderInit, 200);
//上面那样是不行的,网上查了下, 大致是说在setTimeout中this指向window对象,
//于是乎被定时的方法中就使用不到vue的this对象了
//正确示范
//可以无视对ie的支持时
setTimeout(()=> {
this.bidOrderInit();
}, 200);
//需要兼容ie时
setTimeout(function () {
this.bidOrderInit();
}, 200);
来源:https://www.cnblogs.com/yeqrblog/p/8961707.html


猜你喜欢
- 问题:如果一个网站拥有两个域名:domain1.com和domain2.com。在网站运营前期,主推domain1.com,但发展到中期,由
- 联合索引又叫复合索引。对于复合索引:Mysql从左到右的使用索引中的字段,一个查询可以只使用索引中的一部份,但只能是最左侧部分。例如索引是k
- 前言在python中,内置了文件(file)对象,通过一些内置的方法就可以实现对文件的操作,例如open()方法创建一个文件对象,write
- 本文实例讲述了Python处理XML格式数据的方法。分享给大家供大家参考,具体如下:这里的操作是基于Python3平台。在使用Python处
- 1.哆啦A梦“只要把愿望系在竹竿上请求月亮女神,心愿便能达成”。我超喜欢这句话。哆啦A梦的创造要追溯
- 著名的老掉牙的IE6.0在我这里已经有六年工龄了,前几天朋友拿到个IE8.0新的Beta版本,我的Sever2003装不上,大为扫兴。Chr
- 包含Contains(s,substr string) bool判断s是否包含substr true:包含 false:不包含例子: &nb
- 本文实例讲述了go语言使用pipe读取子进程标准输出的方法。分享给大家供大家参考。具体如下:其核心代码如下:cmd := exec.Comm
- 前言上篇我们介绍了微前端实现沙箱的几种方式,没看过的可以下看下JS沙箱这篇内容,扫盲一下。接下来我们通过源 码详细分析下qiankun沙箱实
- 本文实例为大家分享了python实现屏幕中间倒计时的具体代码,供大家参考,具体内容如下先看下效果图:代码:import timefrom t
- JavaScript 循环中,i++ 与 i– 那个比较快?相信有不少朋友看过相关的讨论文章,比如这篇。文章解释了开启优化选项后,i– 的
- 不知不觉2008已经走到了尽头,在这近一年中,一直不断的尝试用ExtJS做项目,从1.1到现在的2.2,吃了不少苦头,也有不少收获,总结一
- QThread是Qt的线程类中最核心的底层类。由于PyQt的的跨平台特性,QThread要隐藏所有与平台相关的代码要使用的QThread开始
- java通过mysql的加解密函数实现敏感字段存储1.AES加解密工具类:public class AESUtils {public sta
- 本文实例讲述了python将MongoDB里的ObjectId转换为时间戳的方法。分享给大家供大家参考。具体分析如下:MongoDB里的_i
- 就像这个图的样子:解决方法,setting.py中DEBUG选项为True,否则无法映射到静态文件目录来源:https://blog.csd
- 在模板中往往要加载静态文件,如CSS, JavaScript,图片等。那么这些文件在django中如何才能正确加载呢?首先要在setting
- 最近在改个程序用到了在js中设置css的float属性,以为和平常的写法一样,原来不是,只好去请教google,原来...首先大家先来看一下
- 在前端调试的时候,跨域一直都是一个比较麻烦的问题,这个在之前的文章 关于跨域问题的一个解决方法 中其实已经讨论了一些可以使用的方法。如果要使
- 首先,创建一个DataFrame格式数据作为举例数据。# 创建一个DataFrame格式数据data = {'a': [