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

相关文章

  • SpringBoot+Vue3实现上传文件功能

    SpringBoot+Vue3实现上传文件功能

    这篇文章主要介绍了SpringBoot+Vue3实现上传文件功能,本文结合示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-01-01
  • vue中data和props的区别详解

    vue中data和props的区别详解

    这篇文章主要介绍了vue中data和props的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习吧
    2024-01-01
  • vue输入框中输完后光标自动跳到下一个输入框中的实现方法

    vue输入框中输完后光标自动跳到下一个输入框中的实现方法

    最近在工作中遇到了些问题,总结下分享给同样遇到这个问题的朋友,这篇文章主要给大家介绍了关于vue输入框中输完后光标自动跳到下一个输入框中的实现方法,需要的朋友可以参考下
    2023-03-03
  • vue实现图片拖拽及鼠标滚轮放大缩小的示例代码

    vue实现图片拖拽及鼠标滚轮放大缩小的示例代码

    本文主要给大家介绍如何vue实现图片拖拽及鼠标滚轮放大缩小,文中有详细的代码供大家参考,对我们的学习或工作有一定的帮助,需要的朋友可以参考下
    2023-08-08
  • Vite vue3多页面入口打包以及部署踩坑实战

    Vite vue3多页面入口打包以及部署踩坑实战

    因为我们公司的项目是多页面应用,不同于传统单页面应用,一个包就可以了,下面这篇文章主要给大家介绍了关于Vite vue3多页面入口打包以及部署踩坑的相关资料,需要的朋友可以参考下
    2022-05-05
  • 一文带你理解 Vue 中的生命周期

    一文带你理解 Vue 中的生命周期

    在我们实际项目开发过程中,会非常频繁地和 Vue 组件的生命周期打交道,接下来我们就从源码的角度来看一下这些生命周期的钩子函数是如何被执行的,需要的朋友可以参考下面文章内容
    2021-09-09
  • el-form表单验证的一些实用方法总结

    el-form表单验证的一些实用方法总结

    表单校验是注册环节中必不可少的操作,表单校验通过一定的规则来确保用户提交数据的有效性,下面这篇文章主要给大家介绍了关于el-form表单验证的一些实用方法,需要的朋友可以参考下
    2023-01-01
  • vue中provide和inject的用法及说明(vue组件爷孙传值)

    vue中provide和inject的用法及说明(vue组件爷孙传值)

    这篇文章主要介绍了vue中provide和inject的用法及说明(vue组件爷孙传值),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • 详解vue2.0 不同屏幕适配及px与rem转换问题

    详解vue2.0 不同屏幕适配及px与rem转换问题

    这篇文章主要介绍了详解vue2.0 不同屏幕适配及px与rem转换问题,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • Vue2.x中的父子组件相互通信的实现方法

    Vue2.x中的父子组件相互通信的实现方法

    这篇文章主要介绍了Vue2.x中的父子组件相互通信,需要的朋友可以参考下
    2017-05-05

最新评论