Vue页面手动刷新,实现导航栏激活项还原到初始状态

 更新时间:2020年08月06日 08:55:00   作者:Lena_叶  
这篇文章主要介绍了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);

以上这篇Vue页面手动刷新,实现导航栏激活项还原到初始状态就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Vue.js结合SortableJS实现树形数据拖拽

    Vue.js结合SortableJS实现树形数据拖拽

    本文主要介绍了Vue.js结合SortableJS实现树形数据拖拽,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • vue组件开发props验证的实现

    vue组件开发props验证的实现

    这篇文章主要介绍了vue组件开发props验证的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-02-02
  • 解决vue-loader加载不上的问题

    解决vue-loader加载不上的问题

    这篇文章主要介绍了解决vue-loader加载不上的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • 基于Vue的文字跑马灯组件(npm 组件包)

    基于Vue的文字跑马灯组件(npm 组件包)

    这篇文章主要介绍了基于Vue的文字跑马灯组件(npm 组件包),需要的朋友可以参考下
    2017-05-05
  • Vue+Echarts实现绘制多设备状态甘特图

    Vue+Echarts实现绘制多设备状态甘特图

    这篇文章主要为大家详细介绍了Vue如何结合Echarts实现绘制多设备状态甘特图,文中的示例代码讲解详细,有需要的小伙伴可以跟随小编一起学习一下
    2024-03-03
  • 关于VUE点击父组件按钮跳转到子组件的问题及解决方案

    关于VUE点击父组件按钮跳转到子组件的问题及解决方案

    本文主要介绍了在Vue框架中,如何通过父组件的点击事件打开子组件中的弹窗并展示表格内容,主要步骤包括在父组件中定义数据属性,创建并定义子组件的弹窗和表格内容,通过props属性和自定义事件实现父子组件间的数据传递和方法调用
    2024-10-10
  • webstorm添加*.vue文件支持

    webstorm添加*.vue文件支持

    这篇文章主要介绍了webstorm添加*.vue文件支持,webstorm很多的插件内置,不用安装插件,下面尝试用vue和es6做项目,有兴趣的可以了解一下
    2018-05-05
  • vue中 render 函数功能与原理分析

    vue中 render 函数功能与原理分析

    这篇文章主要介绍了vue中 render 函数功能与原理,结合实例形式分析了vue中render函数的基本功能、原理及相关操作注意事项,需要的朋友可以参考下
    2023-06-06
  • VUE跨域详解以及常用解决跨域的方法

    VUE跨域详解以及常用解决跨域的方法

    跨域指浏览器不允许当前页面的所在的源去请求另一个源的数据,下面这篇文章主要给大家介绍了关于VUE跨域详解以及常用解决跨域的方法,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2022-07-07
  • vue.js学习笔记:如何加载本地json文件

    vue.js学习笔记:如何加载本地json文件

    这篇文章主要介绍了vue.js学习笔记:如何加载本地json文件,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧。
    2017-01-01

最新评论