当前位置:Gxlcms > JavaScript > vue.js中二级路由和三级路由的代码解析

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

时间:2021-07-01 10:21:17 帮助过:43人阅读

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

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

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

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

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

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

二级路由

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

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

//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"
});

三级路由

和二级路由差不多

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"
});

相关推荐:

Vue.js路由器的使用方法总结(附代码)

vue.js中路由器的配置方法介绍

以上就是vue.js中二级路由和三级路由的代码解析的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行