经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
vue路由--网站导航功能详解
来源:jb51  时间:2019/3/29 16:06:43  对本文有异议

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

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

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

2、定义router的js文件

  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. import User from '../pages/user'
  4. import Home from '../pages/public/home'
  5. import Profile from '../pages/user/profile'
  6. import Form from '../pages/form'
  7. import Detail from '../pages/form/form'
  8. import File from '../pages/form/file'
  9. import Files from '../pages/file'
  10.  
  11. Vue.use(Router)
  12.  
  13. export default new Router({
  14. routes: [
  15. { path: '/', component:Home,
  16. children:[
  17. { path: '/user', component:Profile},
  18. { path: '/profile', component: User},
  19. { path: '/form', component: Form},
  20. { path: '/detail', component: Detail},
  21. { path: '/profiles', component: Files},
  22. { path: '/file', component: File}
  23. ]
  24. },
  25.  
  26. { path: '/login', component:Login},
  27. { path: '/404', component:Error}
  28. ]
  29. })

3、在main.js中引入router

  1. import router from './router'
  2.  
  3. new Vue({
  4. router,
  5. render: h => h(App),
  6. }).$mount('#app')

4、入口页面定义router-view

  1. <div id="app">
  2. <router-view></router-view>
  3. </div>

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

  1. <HeaderSection></HeaderSection>
  2. <div>
  3. <NavList class="nav"></NavList>
  4. <router-view class="router"></router-view>
  5. </div>
  6. <FooterSection></FooterSection>

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

在这里提一下引入elementUI:

(1)安装

npm i element-ui -S

(2)使用

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

  1. import ElementUI from 'element-ui';
  2.  
  3.   import 'element-ui/lib/theme-chalk/index.css';
  4.  
  5.   Vue.use(ElementUI);

导航栏的代码如下:

  1. <el-menu class="sidebar-el-menu" :default-active="onRoutes" :collapse="collapse" background-color="#324157"
  2. text-color="#bfcbd9" active-text-color="#20a0ff" unique-opened router>
  3. <template v-for="item in items">
  4. <template v-if="item.subs">
  5. <el-submenu :index="item.index" :key="item.index">
  6. <template slot="title">
  7. <i :class="item.icon"></i><span slot="title">{{ item.title }}</span>
  8. </template>
  9. <template v-for="subItem in item.subs">
  10. <el-submenu v-if="subItem.subs" :index="subItem.index" :key="subItem.index">
  11. <template slot="title">{{ subItem.title }}</template>
  12. <el-menu-item v-for="(threeItem,i) in subItem.subs" :key="i" :index="threeItem.index">
  13. {{ threeItem.title }}
  14. </el-menu-item>
  15. </el-submenu>
  16. <el-menu-item v-else :index="subItem.index" :key="subItem.index">
  17. {{ subItem.title }}
  18. </el-menu-item>
  19. </template>
  20. </el-submenu>
  21. </template>
  22. <template v-else>
  23. <el-menu-item :index="item.index" :key="item.index">
  24. <i :class="item.icon"></i><span slot="title">{{ item.title }}</span>
  25. </el-menu-item>
  26. </template>
  27. </template>
  28. </el-menu>

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

  1. items: [
  2. {
  3. icon: 'el-icon-share',
  4. index: 'user',
  5. title: '系统首页'
  6. },
  7. {
  8. icon: 'el-icon-time',
  9. index: 'profile',
  10. title: '基础表格'
  11. },
  12. {
  13. icon: 'el-icon-bell',
  14. index: '3',
  15. title: '表单相关',
  16. subs: [
  17. {
  18. index: 'form',
  19. title: '基本表单'
  20. },
  21. {
  22. index: '3-2',
  23. title: '三级菜单',
  24. subs: [
  25. {
  26. index: 'detail',
  27. title: '富文本编辑器'
  28. },
  29. {
  30. index: 'file',
  31. title: 'markdown编辑器'
  32. },
  33. ]
  34. },
  35. {
  36. index: 'profiles',
  37. title: '文件上传'
  38. }
  39. ]
  40. },
  41. ]

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

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号