Vue页面手动刷新,实现导航栏激活项还原到初始状态
作者:Lena_叶 时间:2024-05-03 15:11:44
场景描述:在页面中存在顶部导航和左侧导航,左侧导航和右侧内容区使用了命名视图实现,点击左侧导航的链接时,右侧内容区相应显示不同组件内容。问题:在当前链接手动刷新浏览器(例如:浏览器地址为/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
标签:Vue,导航栏,激活项,初始
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
14道基础Python练习题(附答案)
2023-07-23 02:27:41
![](https://img.aspxhome.com/file/2023/8/110238_0s.png)
Python装饰器中@property使用详解
2022-04-22 04:46:44
Python 异步等待任务集合
2022-08-14 17:23:22
keras 解决加载lstm+crf模型出错的问题
2022-06-11 01:27:27
Javascript中的函数声明与函数表达式(奇技淫巧)
2024-04-23 09:08:43
《Python学习手册》学习总结
2021-09-17 08:55:01
![](https://img.aspxhome.com/file/2023/5/79535_0s.jpg)
python笔记_将循环内容在一行输出的方法
2022-08-11 20:27:09
![](https://img.aspxhome.com/file/2023/7/69517_0s.jpg)
Python编程根据字典列表相同键的值进行合并
2023-09-29 20:10:54
python中k-means和k-means++原理及实现
2022-01-17 10:52:07
![](https://img.aspxhome.com/file/2023/7/66387_0s.png)
Python基础教程之异常详解
2022-02-08 05:48:49
![](https://img.aspxhome.com/file/2023/9/80629_0s.png)
python机器学习理论与实战(一)K近邻法
2021-08-27 18:05:49
![](https://img.aspxhome.com/file/2023/4/90764_0s.jpg)
解决Pycharm界面的子窗口不见了的问题
2022-03-06 15:21:15
![](https://img.aspxhome.com/file/2023/1/65111_0s.jpg)
selenium与xpath之获取指定位置的元素的实现
2022-01-14 12:02:35
![](https://img.aspxhome.com/file/2023/8/134528_0s.jpg)
SQL Select语句完整的执行顺序
2008-09-28 21:22:00
python向字符串中添加元素的实例方法
2023-08-24 00:59:21
![](https://img.aspxhome.com/file/2023/4/97284_0s.jpg)
Linux 下 Python 实现按任意键退出的实现方法
2022-08-07 14:22:01
Python+PyQt5自制监控小工具
2023-09-14 10:52:31
![](https://img.aspxhome.com/file/2023/1/112481_0s.png)
spring boot 不连接数据库启动的解决
2024-01-18 06:38:54
Symfony学习十分钟入门经典教程
2023-11-14 23:38:38
python简单程序读取串口信息的方法
2024-01-02 02:42:18