vue-03-路由-二三级路由跳转

╰+攻爆jí腚メ 2023-07-20 10:35 95阅读 0赞

1、新建项目之后,在 components 文件夹下,新建 about 文件夹

2、引入二级路由,同样的是在main.js 文件下引入,这四个组件都是在 components/about 这个文件夹下,所以路径要写对

  1. //二级路由引入
  2. import Contact from './components/about/Contact'
  3. import Delivery from './components/about/Delivery'
  4. import History from './components/about/History'
  5. import OrderingGuide from './components/about/OrderingGuide'

3、配置二级路由,因为二级路由都是在 about.vue 页面显示的,所以应该写在一级路由 about 的内部,在内部添加一个 children 属性,值为一个数组,数组内部存在多个路由对象,对象内部存在 path 、name、component 三个属性

  1. //配置路由
  2. const routes = [
  3. { path: '/', name:"indexLink", component: Home },
  4. { path: '/menu', name:"menuLink", component: Menu },
  5. { path: '/login', name:"loginLink", component: Login },
  6. { path: '/register', name:"registerLink", component: Register },
  7. { path: '/admin', name:"adminLink", component: Admin },
  8. { path: '/about', name:"aboutLink", component: About ,children:[
  9. { path: '/about/contact', name:'contactLink' , component: Contact },
  10. { path: './delivery', name:'deliveryLink' , component: Delivery },
  11. { path: './history', name:'historyLink' , component: History },
  12. { path: './orderingGuide', name:'orderingGuideLink' , component: OrderingGuide },
  13. ]},
  14. { path: '*', redirect: "/menu" },
  15. ]

页面效果

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNzkyODAw_size_16_color_FFFFFF_t_70

4、配置3级路由。若是还存在三级路由,那就找到对应页面,在对应页面路由的内部继续引入,我现在是在联系我们内部引入的,所以新建一个文件夹,用来盛放三级路由

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNzkyODAw_size_16_color_FFFFFF_t_70 1

5、 重复上述引入二级路由步骤,引入三级路由,注意路由路径。components文件夹下的一级文件,就是一级路由,

about文件夹下的一级文件,就是二级路由,contact文件夹下的一级文件,就是三级路由

  1. //三级路由引入
  2. import Phone from './components/about/Contact/Phone'
  3. import PersionName from './components/about/Contact/PersionName'

6、重复上述配置二级路由步骤,配置三级路由

  1. //配置路由
  2. const routes = [
  3. // 一级路由
  4. { path: '/', name:"indexLink", component: Home },
  5. { path: '/menu', name:"menuLink", component: Menu },
  6. { path: '/login', name:"loginLink", component: Login },
  7. { path: '/register', name:"registerLink", component: Register },
  8. { path: '/admin', name:"adminLink", component: Admin },
  9. { path: '/about', name:"aboutLink", redirect: "/about/contact", component: About ,children:[
  10. // 二级路由
  11. { path: '/about/contact', name:'contactLink' ,redirect:"/about/contact/phoneLink", component: Contact ,children:[
  12. // 三级路由
  13. { path: '/about/contact/phoneLink',name:"phoneLink", component: Phone },
  14. { path: './persionNameLink',name:"persionNameLink", component: PersionName }
  15. ]},
  16. { path: './delivery', name:'deliveryLink' , component: Delivery },
  17. { path: './history', name:'historyLink' , component: History },
  18. { path: './orderingGuide', name:'orderingGuideLink' , component: OrderingGuide },
  19. ]},
  20. { path: '*', redirect: "/menu" },
  21. ]

7、二级路由页面。点击切换二级路由

  1. <template>
  2. <div class="row mb-5">
  3. <div class="col-4">
  4. <!-- 导航 -->
  5. <div class="list-group mb-5">
  6. <router-link tag="li" :to="{name: 'historyLink'}">
  7. <a class="list-group-item list-group-item-action">历史订单</a>
  8. </router-link>
  9. <router-link tag="li" :to="{name: 'contactLink'}">
  10. <a class="list-group-item list-group-item-action">联系我们</a>
  11. </router-link>
  12. <router-link tag="li" :to="{name: 'orderingGuideLink'}">
  13. <a class="list-group-item list-group-item-action">点餐文档</a>
  14. </router-link>
  15. <router-link tag="li" :to="{name: 'deliveryLink'}">
  16. <a class="list-group-item list-group-item-action">快递信息</a>
  17. </router-link>
  18. </div>
  19. </div>
  20. <div class="col-8">
  21. <!-- 导航所对应的内容 点击切换对应的二级路由页面 且 展示-->
  22. <router-view></router-view>
  23. </div>
  24. </div>
  25. </template>

8、三级路由页面。点击切换三级路由

  1. <template>
  2. <div class="card text-dark bg-light mb-3">
  3. <div class="card-header">联系我们</div>
  4. <div class="card-body">
  5. <h4 class="card-title">联系我们</h4>
  6. <p class="cad-text">3104527996@qq.com</p>
  7. <div>
  8. <router-link :to="{name :'phoneLink'}">电话</router-link>
  9. <router-link :to="{name :'persionNameLink'}">联系人</router-link>
  10. <!-- 点击上面的 router-link 切换对应三级路由 且 展示-->
  11. <router-view></router-view>
  12. </div>
  13. </div>
  14. </div>
  15. </template>

此时,页面已经可以正常切换二级和三级路由了,但是在二级和三级路由中,没有设置默认显示页面,所以在配置 二级 和 三级 路由时设置 redirect 属性,以此来指定显示的二级 和三级 路由, ps:但是,需要注意的是,在一级路由里指定二级路由时,需要是完全一样的路径,其实

path: ‘./delivery’, 和 path: ‘/about/contact’, 两个在页面中显示的路径都是一样的,但是指定的时候必须要写成 path: ‘/about/contact’, 这样的

  1. //配置路由
  2. const routes = [
  3. { path: '/', name:"indexLink", component: Home },
  4. { path: '/menu', name:"menuLink", component: Menu },
  5. { path: '/login', name:"loginLink", component: Login },
  6. { path: '/register', name:"registerLink", component: Register },
  7. { path: '/admin', name:"adminLink", component: Admin },
  8. { path: '/about', name:"aboutLink", redirect: "/about/contact", component: About ,children:[
  9. { path: '/about/contact', name:'contactLink' ,redirect:"/about/contact/phoneLink", component: Contact ,children:[
  10. { path: '/about/contact/phoneLink',name:"phoneLink", component: Phone },
  11. { path: './persionNameLink',name:"persionNameLink", component: PersionName }
  12. ]
  13. },
  14. { path: './delivery', name:'deliveryLink' , component: Delivery },
  15. { path: './history', name:'historyLink' , component: History },
  16. { path: './orderingGuide', name:'orderingGuideLink' , component: OrderingGuide },
  17. ]},
  18. { path: '*', redirect: "/menu" },
  19. ]

还有就是,路径配置时,直接写 /about 类似的就是直接在根目录下的,若是写成 ./delivery 类似的,前面加 . 的,就代表是在当前路径下

发表评论

表情:
评论列表 (有 0 条评论,95人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue

    [【Vue】路由跳转][Vue] 路由跳转有几种方式,我用的最多的是$router.push的方法: 实践: 例如下面的页面,要求点详情跳转到详情页。 ![在这里

    相关 Vue

    路由跳转有几种方式,我用的最多的是$router.push的方法: 实践: 例如下面的页面,要求点详情跳转到详情页。 ![在这里插入图片描述][watermark_

    相关 Vue+传参

    之前在原生JS的开发中,我们经常会用到根据某一状态进行页面的跳转。 比如:登录成功跳到首页,点击商品列表的某个商品跳转商品详情等。 而常见的写法就是: locat