栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

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

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

1、我们继续上一个案例 vue -- 路由精讲制作导航 -- 从无到有 ,在 about文件夹下 创建一些文件夹。如下:

2、编写about.vue代码。当我们点击导航中 “关于我们” ,就会显示该部分内容。代码中写了四个可供点击后可以跳转的模块。和 表示你点击了哪个组件,哪个组件就会渲染到这里来。

其中注意:css样式,我们直接引入bootstrap中的导航的样式,在标签中直接添加class属性的值就可以了。

about.vue代码

3、配置二级路由和三级路由

注意:我们要在about页面下添加四个路由,用到 children 属性, 而且还用到 redirect 属性,默认跳转到指定路由,具体操作看代码和注释。

main.js代码

import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App.vue'
import Home from './components/Home.vue'
import Menu from './components/Menu.vue'
import Admin from './components/Admin.vue'
import about from './components/about/about.vue'
import Login from './components/Login.vue'
import Register from './components/Register.vue'

//二级路由
import Contact from './components/about/Contact.vue'
import Delivery from './components/about/Delivery.vue'
import History from './components/about/History.vue'
import OrderingGuide from './components/about/OrderingGuide.vue'

//三级路由
import Phone from './components/about/contact/Phone.vue'
import PersonName from './components/about/contact/PersonName.vue'

Vue.use(VueRouter)

核心代码 二级路由和三级路由的跳转

const routes = [
  {path:'/',   name:'homelink',  component:Home},
  {path:'/menu', name:'menulink',  component:Menu},
  {path:'/admin', name:'adminlink', component:Admin},
  {path:'/about', name:'aboutlink', redirect:'/about/contact', component:about, children:[      
                       //表示about页面中默认跳转到/about/contact 这个路由页面下。
    {path:'/about/contact', name:'contactlink', redirect:'/personName', component:Contact, children:[
                       //在/about/contact页面中默认展现三级路由personName 的内容。
      {path:'/phone', name:"phoneNumber", component:Phone},
      {path:'/personName', name:"personName", component:PersonName},
    ]},
    {path:'/history',name:'historylink',component:History},
    {path:'/delivery',name:'deliverylink',component:Delivery},
    {path:'/orderingGuide',name:'orderingGuidelink',component:OrderingGuide},
  ]},
  {path:'/login', name:'loginlink', component:Login},
  {path:'/register', name:'registerlink', component:Register},
  // {path:'*',redirect:'/'},
]

const router = new VueRouter({
  routes,
  mode:'history'
})

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

Contact.vue代码


Delivery.vue代码

History.vue代码

OrderingGuide.vue代码

Phone.vue代码

PersonName.vue代码

补充知识:vue:菜单收缩功能

想要实现:点击菜单能收缩。(效果如下:)

点击前:

点击后:

思路:

首先我们要知道绅缩的按钮和菜单是否在一个页面。在一个页面就简单了。

如果不在一个页面,就会涉级到父子级传参,绅缩按钮模块中要把状态传给header,这是兄弟间的传递参数,需要用到 vuex。如果不用vuex的话,就通过主体去操作。绅缩按钮把状态传给主体是子传父,通过 this.$emit()。主体把状态传给菜单,是父传子,通过props ,菜单中需要接收主体中传过来的东西,要在 data 中定义props 在里面定义type、required、default。如果不清楚props 是啥,请百度。

操作:

1、先看整体布局

2、menu 模块

3、header 模块

以上这篇vue-路由精讲 二级路由和三级路由的作用就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/69181.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号