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页面手动刷新,导航栏激活项还原到初始状态问题解决方案的更多相关文章
- 前端Vue项目——初始化及导航栏
一.项目初始化 创建webpack模板项目如下所示: MacBook-Pro:PycharmProjects hqs$ vue init webpack luffy_project ? Project ...
- iOS7 下使用SVPullToRefresh 下拉刷新导航栏位置错误
iOS7 下使用SVPullToRefresh 下拉刷新导航栏位置错误: 下拉刷新之后,tableview的第一列会跑到导航栏的下面: 修正:添加如下代码 /** * 下拉刷新 增加一个: */ // ...
- Vue 实现手动刷新组件
Vue 实现手动刷新组件:https://www.jianshu.com/p/742142dc95f3
- Django进入监听端口就自动打开指定页面,无需导航栏手动添加(Django六)
在我们进入监听端口时画面如下:而因为在urls.py中写过如下语句 我们在监听端口后加上/login就会跳转到login.html页面,如下图 那么如何一打开监听端口就可以走动跳转到login.htm ...
- vue-router+elelment-ui,实现导航栏激活高亮
<el-menu :default-active="$route.path" class="el-menu-vertical-demo" backgrou ...
- Vue 页面 前进刷新 后退不刷新(keepAlive)
前言 遇到这一个个问题 需要是这样的 Vue里面的不刷新问题 页面分为: A 主页 B列表页 C 详情页 A beforeRouteLeave 时设置 to.meta.keepAlive = ...
- 前端 vue单页面应用刷新网页后vuex的state数据丢失的解决方案(转载)
最近接手了一个项目,前端后端都要做,之前一直在做服务端的语言.框架和环境,前端啥都不会啊. 突然需要前端编程,两天速成了JS和VUE框架,可惜还是个半吊子.然后遇到了一个困扰了一整天的问题.一直调试都 ...
- vue单页面应用刷新网页后vuex的state数据丢失的解决方案
1. 产生原因其实很简单,因为store里的数据是保存在运行内存中的,当页面刷新时,页面会重新加载vue实例,store里面的数据就会被重新赋值. 2. 解决思路一种是state里的数据全部是通过请求 ...
- ionic2 跳转子页面隐藏底部导航栏
第一种方法: 在tab里面添加一个属性[tabsHideOnSubPages]='true' <ion-tab [root]="tab1Root" [tabsHideOnSu ...
随机推荐
- SQL-W3School-高级:SQL 数据库
ylbtech-SQL-W3School-高级:SQL 数据库 1.返回顶部 1. 现代的 SQL 服务器构建在 RDBMS 之上. DBMS - 数据库管理系统(Database Managemen ...
- ubuntu下编译和使用libxml2
安装: #sudo apt-get install libxml2 #sudo apt-get install libxml2-dev sudo apt-get install libxml2-dev ...
- Carve Visual Studio2015编译
下载Carve库 https://github.com/folded/carve 目录结构如下: 用Visual Studio2015打开,点击右键,生成即可 在bin目录下生成了 .lib文件 ...
- 123457123457#0#---------com.ppGame.SeaPuzzleGame73--前拼后广--宝宝海洋拼图pp
com.ppGame.SeaPuzzleGame73--前拼后广--宝宝海洋拼图pp
- Apache配置优化之开启KeepAlive
在HTTP 1.0中和Apache服务器的一次连接只能发出一次HTTP请求,而KeepAlive参数支持HTTP 1.1版本的一次连接,多次传输功能,这样就可以在一次连接中发出多个HTTP请求.从而避 ...
- Tools - Tcpdump
Tcpdump homepage - tcpdump wiki - tcpdump 常用格式 tcpdump -i eth<网卡号> port <端口号> -s0 -w < ...
- 使用ASP.NET Core支持GraphQL( restful 配套)
https://github.com/graphql-dotnet https://github.com/graphql GraphQL简介 官网:https://graphql.cn/code/ 下 ...
- Introduction - What is machine learning
摘要: 本文是吴恩达 (Andrew Ng)老师<机器学习>课程,第一章<绪论:初识机器学习>中第2课时<什么是机器学习?>的视频原文字幕.为本人在视频学习过程中逐 ...
- idea用maven创建web项目(详细)
引用:http://blog.csdn.net/u010361662/article/details/50605099 欢迎添加微信
- Spark简介 --大数据
一.Spark是什么? 快速且通用的集群计算平台 二.Spark的特点: 快速:Spark扩充流行的Mapreduce计算模型,是基于内存的计算 通用:Spark的设计容纳了其它分布式系统拥有的功能, ...