vue.js中二级路由和三级路由的代码解析

vue.js中二级路由和三级路由的代码解析

 

本篇文章给大家带来的内容是关于vue.js中二级路由和三级路由的代码解析 ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

 

为什么要用二级和三级路由?

当项目中 有多个 <router-view> 时,就必须使用分级路由;

如果路由不分级,又有多个 <router-view> ,全部都是定义为一级路由,那么项目中的 <router-view> 的内容显示就会混乱;这是不友好的;

如果对路由进行分级,那么当触发某个二级或三级路由时,此路由就会将对应组件内容传给自己的父级路由组件里面的 <router-view>,这样就不会混乱;

一级路由被触发时,自然会找自己所注册的根组件的<router-view>

二级路由

为一级路由添加一个 children 属性数组,并将二级路由放入;

path 属性 决定 跳转后 url 地址栏的的显示

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

//main.js

//一级路由

import About from './components/about/About'

 

 

//二级路由

import Contact from './components/about/Contact'

import Delivery from './components/about/Delivery'

import History from './components/about/History'

import OrderingGuide from './components/about/OrderingGuide'

 

const router= new VueRouter({

  routes:[

    {path:'/about',name:'about',component:About,children:[

        {path:'/about/contsssssssssssssssact',name:'contactLink',component:Contact},

        {path:'/history',name:'historyLink',component:History},

        {path:'/',name:'deliveryLink',component:Delivery},

        {path:'/orderingGuide',name:'orderingGuideLink',component:OrderingGuide},

      ]},

    {path:'*',redirect:'/'}

  ],

  mode:"history"

});

三级路由

和二级路由差不多

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

const router= new VueRouter({

  routes:[

    {path:'/',name:'home',component:Home},

    {path:'/menu',name:'menu',component:Menu},

    {path:'/admin',name:'admin',component:Admin},

    {path:'/about',name:'about',component:About,redirect: {name:'contactLink'},children:[   //二级路由

        {path:'/about/contact',name:'contactLink',component:Contact},

        {path:'/history',name:'historyLink',component:History},

        {path:'/delivery',name:'deliveryLink',component:Delivery},

        {path:'/orderingGuide',name:'orderingGuideLink',component:OrderingGuide,redirect:{name:'phonelink'},children: [  //三级路由

            {path:'/phone',name:'phonelink',component:Phone},

            {path:'/name',name:'namelink',component:Name}

          ]},

      ]},

    {path:'/login',name:'login',component:Login},

    {path:'/register',name:'register',component:Register},

    {path:'*',redirect:'/'}

  ],

  mode:"history"

});

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值