通过vue刷新左侧菜单栏操作


Posted in Javascript onAugust 06, 2020

今天完成了手头任务就想着做点什么,刚好领导让我看看项目左侧菜单栏不刷新的问题,我也是刚刚接触vue,很多东西都还不是很熟练,这也是我的第一篇自己写的博客,感觉还是很兴奋的,我觉得写博客这个习惯要一直养成,不但总结了自己一天的工作所得,而且也是对自己的一种良好习惯的养成。

下面进入正题。

这个是我们html里面的超链接,而我们的点击事件的跳转就是通过这个超链接实现的。

<el-menu-item index="3-1"><a href="#/commodity-list" rel="external nofollow" >

然后我们要创建一个js文件,将我们要跳转的路径导入

import ChannelList from './src/commodity-manage/channel-list/channel-list'

配置路由管理:

const router = new VueRouter({
 routes: [
 {
   path: '/commodity-list',
   name: 'commodity-list',
   component: commodityStorage,
   children: []
  }
]

path:就是我们要跳转的路径

name:跳转文件的名字

component:配置了映射的组件

在html文件中配置了<router-view/>

<router-view :key="key"></router-view>

是用来渲染通过路由映射过来的组件,当路径更改时,<router-view> 中的内容也会发生更改

在js文件中使用computed来进行监听

//每次让路由生成不同的值,用于重新加载组件,达到刷新数据的效果
  computed: {
   key() {
     return this.$route.name !== undefined? this.$route.name +new Date(): this.$route +new Date()
   }
  },

补充知识:vue:路由菜单(element 和 antd)

在 vue 中 使用 UI框架中的菜单,给菜单如何添加路由呢?其中会出现路由样式的问题。请看下面两种UI方法。

注)使用框架的时候注入知道的吧。。。。。防止有些人xxxx,我还是写一下。

通过vue刷新左侧菜单栏操作

场景:使用 elementUI 的 NavMenu 时。

这里请注意:可以不使用 router-link,在 e-menu 上面绑定 route 或者 :route = 'true' ,然后遍历的时候 :index=‘route.path' (:index=‘路径')。

通过vue刷新左侧菜单栏操作

代码

<template>
  <div class="menu">
    <el-menu default-active='activePath'
         router
         @open='handleOpen'
         @close='handleClose'
         background-color='#545c64'
         text-color='#fff'
         active-text-color='#ffd04b' >
      <template v-for="(route,index) in routes">
        <!-- 一级菜单 -->
        <el-menu-item :key='index' v-if='route.children && route.children.length== 1' :index='route.path'>
          <i :class="'el-icon-' + route.meta.icon"></i>
          <span>{{route.meta.title}}</span>
        </el-menu-item>
 
        <!-- 二级菜单 -->
        <el-submenu v-if='route.children && route.children.length > 1' :key='index' :index='route.path'>
          <template slot='title'>
            <i :class="'el-icon-' + route.meta.icon"></i>
            {{route.meta.title}}
          </template>
          <el-menu-item-group v-for='(item, index) in route.children'>
            <el-menu-item :key='index' :index='resolve(route.path, item.path)'>
              <i :class="'el-icon-' + item.meta.icon"></i>
              {{item.meta.title}}
            </el-menu-item>
          </el-menu-item-group>
        </el-submenu>
      </template>
    </el-menu>
  </div>
</template>
 
<script>
 
export default {
  name: 'Menu',
  data() {
   return {
     activePath: this.$router.path,
   }
  },
  computed: { // 计算属性:获取路由
    routes() {
      console.log('test', this.$router)
      console.log('ddd', this.$router.options.routes)
      return this.$router.options.routes
    },
  },
  methods: {
    resolve(p,i){
     return `${p}/${i}`
    },
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    }
  },
}
</script>
 
<style lang='less'>
  .el-menu {
    text-align: left;
  }
</style>

场景:使用 antd 的 Menu 时。

这个里面是需要使用route-link做路由跳转的。

通过vue刷新左侧菜单栏操作

代码

<template>
  <div class="menu">
   <a-menu v-model="current" mode="inline" theme="dark">
     <template v-for='route in routes'>
       <!-- 一级菜单 -->
       <a-menu-item v-if='route.children && route.children.length == 1' :key='route.path'>
        <router-link :to='route.path'>
          <a-icon :type='route.meta.icon' />
          {{ route.meta.title }}
        </router-link>
       </a-menu-item>
 
       <!-- 二级菜单 -->
       <a-sub-menu v-else='route.children && route.children.length == 2' key="sub1">
        <span slot="title"><span><a-icon :type='route.meta.icon' />{{ route.meta.title}}</span></span>
        <a-menu-item v-for='item in route.children' :key='item.path'>
          <router-link :to='resolve(route.path,item.path)'>
          <!-- <router-link :to="`${route.path}/${item.path}`"> -->
            <a-icon :type='item.meta.icon' />
            {{ item.meta.title }}
          </router-link>
        </a-menu-item>
       </a-sub-menu>
     </template>
   </a-menu>
  </div>
</template>
 
<script>
 
export default {
  name: 'Menu',
  data() {
   return {
     current: ['/'],
   }
  },
  computed: { // 计算属性:获取路由
    routes() {
      console.log('test', this.$router)
      console.log('ddd', this.$router.options.routes)
      return this.$router.options.routes
    },
  },
  methods:{
    resolve(p,i){
     return `${p}/${i}`
    },
  },
}
</script>

以上这篇通过vue刷新左侧菜单栏操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
js setTimeout()函数介绍及应用以倒计时为例
Dec 12 Javascript
javaScript年份下拉列表框内容为当前年份及前后50年
May 28 Javascript
javascript日期格式化方法汇总
Oct 04 Javascript
原生js实现图片层叠轮播切换效果
Feb 02 Javascript
JavaScript实现邮箱地址自动匹配功能代码
Nov 28 Javascript
JavaScript中利用for循环遍历数组
Jan 15 Javascript
jQuery实现复制到粘贴板功能
Feb 11 Javascript
基于mpvue的小程序项目搭建的步骤
May 22 Javascript
vue如何截取字符串
May 06 Javascript
自定义Vue组件打包、发布到npm及使用教程
May 22 Javascript
vue移动端实现手机左右滑动入场动画
Jun 17 Javascript
js实现鼠标拖曳效果
Dec 30 Javascript
Vue页面手动刷新,实现导航栏激活项还原到初始状态
Aug 06 #Javascript
浅谈JavaScript中等号、双等号、 三等号的区别
Aug 06 #Javascript
前端开发基础javaScript的六大作用
Aug 06 #Javascript
vue 导航菜单刷新状态不消失,显示对应的路由界面操作
Aug 06 #Javascript
解决vue-router路由拦截造成死循环问题
Aug 05 #Javascript
Vue基于iview table展示图片实现点击放大
Aug 05 #Javascript
vue相同路由跳转强制刷新该路由组件操作
Aug 05 #Javascript
You might like
Extended CHM PHP 语法手册之 DIY
2006/10/09 PHP
smarty+adodb+部分自定义类的php开发模式
2006/12/31 PHP
php中将字符串转为HTML的实体引用的一个类
2013/02/03 PHP
zend framework文件上传功能实例代码
2013/12/25 PHP
php+mysql数据库实现无限分类的方法
2014/12/12 PHP
php使用strip_tags()去除html标签仍有空白的解决方法
2016/07/28 PHP
Javascript !!的作用
2008/12/04 Javascript
获取3个数组不重复的值的具体实现
2013/12/30 Javascript
Javascript实现单张图片浏览
2014/12/18 Javascript
php利用curl获取远程图片实现方法
2015/10/26 Javascript
Bootstrap CSS组件之导航条(navbar)
2016/12/17 Javascript
JS实现json对象数组按对象属性排序操作示例
2018/05/18 Javascript
浅谈vue中使用编辑器vue-quill-editor踩过的坑
2020/08/03 Javascript
Python实现获取某天是某个月中的第几周
2015/02/11 Python
简单介绍Python中的struct模块
2015/04/28 Python
解决python3 json数据包含中文的读写问题
2018/05/10 Python
python抖音表白程序源代码
2019/04/07 Python
浅谈Python中(&amp;,|)和(and,or)之间的区别
2019/08/07 Python
python中struct模块之字节型数据的处理方法
2019/08/27 Python
如何在 Django 模板中输出 &quot;{{&quot;
2020/01/24 Python
快速查找Python安装路径方法
2020/02/06 Python
在tensorflow下利用plt画论文中loss,acc等曲线图实例
2020/06/15 Python
html5实现九宫格抽奖可固定抽中某项奖品
2020/06/15 HTML / CSS
印度尼西亚在线时尚购物网站:ZALORA印尼
2016/08/02 全球购物
Watch Station官方网站:世界一流的手表和智能手表
2020/01/05 全球购物
销售人员职业生涯规划范文
2014/03/01 职场文书
2014公司党员自我评价范文
2014/09/11 职场文书
公务员四风问题对照检查材料整改措施
2014/09/26 职场文书
2014年司机工作总结
2014/11/21 职场文书
2014年仓库管理工作总结
2014/12/17 职场文书
市场部经理岗位职责
2015/02/02 职场文书
消防安全月活动总结
2015/05/08 职场文书
小学教学工作总结2015
2015/05/13 职场文书
基于go interface{}==nil 的几种坑及原理分析
2021/04/24 Golang
Java数组与堆栈相关知识总结
2021/06/29 Java/Android
深入理解go slice结构
2021/09/15 Golang