美文网首页
Vue 路由嵌套 二级路由和三级路由

Vue 路由嵌套 二级路由和三级路由

作者: 祝名 | 来源:发表于2018-12-29 18:28 被阅读0次

1. main.js文件下二级路由和三级路由的引入,以及如何定义他们的路径path、名称name、调用组件component、默认展示组件redirect。

  • 路由对象数组routes中存放着所有路由的路径和组件。在这里二级路由,以children数组的形式挂在一级路由对象里面,如下所示。
  • 三级路由也挂在二级路由对象中的children数组里面。
  • redirect挂在拥有二级路由的一级组件里表示,当我们打开这个一级路由时,默认展示redirect的路径值,也就是他其中一个二级路由。
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import Home from './components/Home'
import Menu from './components/Menu'
import Admin from './components/Admin'
import About from './components/about/About'
import Login from './components/Login'
import Register from './components/Register'
// 二级路由
import Contact from './components/about/Contact'
import Delivery from './components/about/Delivery'
import History from './components/about/History'
import OrderingGuide from './components/about/OrderingGuide'
//三级路由
import Phone from './components/about/contact/Phone'
import PersonName from './components/about/contact/PersonName'
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",component:About,redirect:'/about/contact',children:[
    {path:'/about/contact',name:"contactLink",redirect:'/about/contact/personname',component:Contact,children:[
      {path:'/about/contact/phone',name:'phoneNumber',component:Phone},
      {path:'/about/contact/personname',name:'personName',component:PersonName},
    ]},
    {path:'/about/delivery',name:"deliveryLink",component:Delivery},
    {path:'/about/history',name:"historyLink",component:History},
    {path:'/about/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({
  router,
  el: '#app',
  render: h => h(App)
})

2. 如上所示,contact、delivery、history、orderingGuide二级路由挂在一级路由about下。phone、personname三级路由挂在二级路由contact下面。

  • about组件代码如下
<template>
    <div>
        <div class="row mb-5">
            <div class="col-4">
                <!-- 导航 -->
                <div class="list-group mb-5">
                    <router-link tag="li" class="nav-link" :to="{name:'historyLink'}">
                        <a class="list-group-item list-group-item-action">历史订单</a>
                    </router-link>
                    <router-link tag="li" class="nav-link" :to="{name:'contactLink'}">
                        <a class="list-group-item list-group-item-action">联系我们</a>
                    </router-link>
                    <router-link tag="li" class="nav-link" :to="{name:'orderingGuideLink'}">
                        <a class="list-group-item list-group-item-action">点餐文档</a>
                    </router-link>
                    <router-link tag="li" class="nav-link" :to="{name:'deliveryLink'}">
                        <a class="list-group-item list-group-item-action">快递信息</a>
                    </router-link>
                </div>
            </div>
            <div class="col-8">
                <!-- 导航所对应的内容 -->
                <router-view></router-view>
            </div>
        </div>
    </div>
</template>
  • contact组件代码如下
<template>
    <div class="card text-dark bg-light mb-3">
        <div class="card-header">联系我们</div>
        <div class="card-body">
            <h4 class="card-title">联系我们</h4>
            <p class="card-text">572404261@qq.com</p>
            <router-link :to="{name:'phoneNumber'}">电话</router-link>
            <router-link :to="{name:'personName'}">联系人</router-link>

            <router-view></router-view>
        </div>
    </div>
</template>

相关文章

  • vue中的路由

    vue中4中路由包含: 1、动态路由2、嵌套路由3、编程式路由4、命名式路由 1、动态路由 2、嵌套路由 3、编程...

  • Vue 路由嵌套 二级路由和三级路由

    1. main.js文件下二级路由和三级路由的引入,以及如何定义他们的路径path、名称name、调用组件comp...

  • vue路由

    路由:vue-router 案例: 1.路由 2.路由的嵌套

  • vue嵌套路由与404重定向实现方法分析

    第一部分: vue嵌套路由 嵌套路由是什么? 嵌套路由就是在一个被路由过来的页面下可以继续使用路由,嵌套也就是路由...

  • 6 VUE路由

    vue-> SPA应用,单页面应用(引入vue-router.js) 路由嵌套(多层路由): 路由其他信息:

  • 12.Vue嵌套路由(三层)

    Vue嵌套路由:实现效果(路由三层嵌套,点击一级tab显示二级tab效果,二级tab点击切换对应内容,不在tab区...

  • vue-router

    前端路由的基本原理 vue-router的基本使用 命名路由 路由参数 嵌套路由

  • VUE路由相关知识

    配置路由 1、二级路由2、三级路由3、若路由错误跳转到统一404页面4、路由传参

  • vue 第八章

    路由1.路由下载用gitnpm install vuenpm install vue-router 2.路由嵌套嵌...

  • vue项目中使用vue-router进行路由配置及嵌套多级路由

    在vue项目中,很多时候我们需要二级路由或者三级路由来跳转页面,但大部分需求下我们都只用到了二级路由,有小伙伴就会...

网友评论

      本文标题:Vue 路由嵌套 二级路由和三级路由

      本文链接:https://www.haomeiwen.com/subject/hkcplqtx.html