vue路由--网站导航功能详解

 更新时间:2019年03月29日 15:01:26   作者:薄荷凉凉凉  
这篇文章主要介绍了vue路由--网站导航功能详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1、首先需要按照Vue router支持

npm install vue-router
然后需要在项目中引入:

import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

2、定义router的js文件

import Vue from 'vue'
import Router from 'vue-router'
import User from '../pages/user'
import Home from '../pages/public/home'
import Profile from '../pages/user/profile'
import Form from '../pages/form'
import Detail from '../pages/form/form'
import File from '../pages/form/file'
import Files from '../pages/file'

Vue.use(Router)

export default new Router({
 routes: [
  { path: '/', component:Home,
   children:[
    { path: '/user', component:Profile},
    { path: '/profile', component: User},
    { path: '/form', component: Form},
    { path: '/detail', component: Detail},
    { path: '/profiles', component: Files},
    { path: '/file', component: File}
   ]
  },

  { path: '/login', component:Login},
  { path: '/404', component:Error}
 ] 
})

3、在main.js中引入router

import router from './router'

new Vue({
 router,
 render: h => h(App),
}).$mount('#app')

4、入口页面定义router-view

<div id="app">
 <router-view></router-view>
 </div>

5、在path指向为“/”的页面中,定义页面的布局,例如:上(头部)-中(左道航-右内容)-下(底部)。

<HeaderSection></HeaderSection>
 <div>
  <NavList class="nav"></NavList>
  <router-view class="router"></router-view>
 </div>
<FooterSection></FooterSection>

6、左侧导航,用elementUI实现,有一个NavMenu导航菜单,做导航功能。

在这里提一下引入elementUI:

(1)安装

npm i element-ui -S

(2)使用

在main.js中加入下面的代码:

import ElementUI from 'element-ui';

  import 'element-ui/lib/theme-chalk/index.css';

  Vue.use(ElementUI);

导航栏的代码如下:

<el-menu class="sidebar-el-menu" :default-active="onRoutes" :collapse="collapse" background-color="#324157"
     text-color="#bfcbd9" active-text-color="#20a0ff" unique-opened router>
 <template v-for="item in items">
  <template v-if="item.subs">
   <el-submenu :index="item.index" :key="item.index">
    <template slot="title">
    <i :class="item.icon"></i><span slot="title">{{ item.title }}</span>
    </template>
    <template v-for="subItem in item.subs">
    <el-submenu v-if="subItem.subs" :index="subItem.index" :key="subItem.index">
     <template slot="title">{{ subItem.title }}</template>
     <el-menu-item v-for="(threeItem,i) in subItem.subs" :key="i" :index="threeItem.index">
      {{ threeItem.title }}
     </el-menu-item>
    </el-submenu>
    <el-menu-item v-else :index="subItem.index" :key="subItem.index">
     {{ subItem.title }}
    </el-menu-item>
    </template>
   </el-submenu>
  </template>
  <template v-else>
   <el-menu-item :index="item.index" :key="item.index">
    <i :class="item.icon"></i><span slot="title">{{ item.title }}</span>
   </el-menu-item>
  </template>
 </template>
</el-menu>

定义左侧导航的显示和图标等内容,index为唯一标识,打开的是path路径,对应router中的path,就可以打开写好的相应的页面。

items: [
     {
      icon: 'el-icon-share',
      index: 'user',
      title: '系统首页'
     },
     {
      icon: 'el-icon-time',
      index: 'profile',
      title: '基础表格'
     },
     {
      icon: 'el-icon-bell',
      index: '3',
      title: '表单相关',
      subs: [
       {
        index: 'form',
        title: '基本表单'
       },
       {
        index: '3-2',
        title: '三级菜单',
        subs: [
         {
          index: 'detail',
          title: '富文本编辑器'
         },
         {
          index: 'file',
          title: 'markdown编辑器'
         },
        ]
       },
       {
        index: 'profiles',
        title: '文件上传'
       }
      ]
     },
    ]

7、如果涉及到登录页面和不需要路由的页面等,就需要在router的js文件中定义和“/”平级的其他path的页面,再判断进入页面是路由页面还是登录等页面。

以上所述是小编给大家介绍的vue路由--网站导航功能详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • vue实现简易的计算器功能

    vue实现简易的计算器功能

    这篇文章主要为大家详细介绍了vue实现简易的计算器功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • Vue.js实现文件上传和进度条显示功能

    Vue.js实现文件上传和进度条显示功能

    在现代Web开发中,文件上传是一个常见而重要的需求,无论是在用户上传头像、文档或者其他类型的文件时,良好的用户体验都是至关重要的,在这篇博客中,我们将介绍如何使用Vue.js来实现文件上传功能,同时显示上传进度条,需要的朋友可以参考下
    2024-11-11
  • 一文带你深入理解Vue3响应式原理

    一文带你深入理解Vue3响应式原理

    响应式就是当对象本身(对象的增删值)或者对象属性(重新赋值)发生变化时,将会运行一些函数,最常见的就是render函数,下面这篇文章主要给大家介绍了关于Vue3响应式原理的相关资料,需要的朋友可以参考下
    2022-11-11
  • vue项目启动时无法识别es6的扩展语法的解决

    vue项目启动时无法识别es6的扩展语法的解决

    启动项目时遇到ES6的拓展运算符报错可以通过切换到淘宝镜像,以及在项目根目录下新增.babelrc和postcss.config.js文件来解决,这些操作有助于正确配置项目环境,从而避免报错,并保证项目的顺利运行,希望这些经验能够帮助到遇到相同问题的开发者
    2024-10-10
  • vue中$emit的用法详解

    vue中$emit的用法详解

    这篇文章主要介绍了vue中$emit的用法详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08
  • 基于Vue3实现鼠标滑动和滚轮控制的轮播

    基于Vue3实现鼠标滑动和滚轮控制的轮播

    在这篇文章主要为大家详细介绍了如何一步步地实现一个基于 Vue 3 的轮播组件,这个组件的特点是可以通过鼠标滑动和滚轮来控制轮播图的切换,感兴趣的可以了解下
    2024-02-02
  • vue+iview实现文件上传

    vue+iview实现文件上传

    这篇文章主要为大家详细介绍了vue+iview实现文件上传,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-11-11
  • 前端全屏显示解决方案分享(附图文详解)

    前端全屏显示解决方案分享(附图文详解)

    这篇文章主要介绍了前端实现全屏显示的三种解决方案,并针对每种方案的适用场景和优缺点进行了详细分析,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2025-01-01
  • vue-路由精讲 二级路由和三级路由的作用

    vue-路由精讲 二级路由和三级路由的作用

    这篇文章主要介绍了vue-路由精讲 二级路由和三级路由的作用,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • 浅析Vue中权限管理的实现

    浅析Vue中权限管理的实现

    在前端开发中,权限管理是一项至关重要的任务,本教程将深入探讨如何在 Vue.js 项目中实施权限管理,并详细讲解如何实现到按钮级别的细粒度控制,希望对大家有所帮助
    2024-11-11

最新评论