嵌套路由就是路由里面嵌套他的子路由,可以有自己的路由导航和路由容器(router-link、router-view),通过配置children可实现多层嵌套
//mine组件 <template> <div class="content"> 在mine的组件里面嵌套路由 <router-link to="/mine/c">去到Cpage</router-link> <router-link to="/mine/d">去到Dpage</router-link> <div class="child"> <router-view></router-view> </div> </div> </template>
//router.js
//引入需要的组件,下是我的路径
import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/home'
import Mine from '@/components/mine'
import Cpage from '@/page/mine/c'
import Dpage from '@/page/mine/d'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
redirect: 'home'
},
{
path: '/home',
name: 'home',
component: Home
},
{
path: '/mine',
name: 'Mine',
component:Mine,
children:[
{
path:'/',
component:Cpage,
},
{
path:'/mine/c',
component:Cpage,
},
{
path:'/mine/d',
component:Dpage,
}
]
//children这是嵌套的部分
},
//c.vue <template> <div class="top-80"> c.vue <p>这里Cpage文件</p> </div> </template> //d.vue <template> <div class="top-80"> d.vue <p>这里Dpage文件</p> </div> </template>
我所遇到的坑
//route.js const App = () => import('../App.vue'); const Login = () => import('../component/Login.vue'); const Class = () => import('../component/Class.vue'); const CourseList = () => import('../component/CourseList.vue'); const CourseContent = () => import('../component/CourseContent.vue'); const routers = [{ path:'/', component:App, children:[{ path:'login', component:Login },{ path:'class', component:Class }, { path:'course', children:[{ path:'list', component:CourseList },{ path:'content', component:CourseContent } ] }, ] }] export default routers
当你访问的时候,发现
http://localhost:8080/#/login
http://localhost:8080/#/class
都正常,但是:
http://localhost:8080/#/course/list
http://localhost:8080/#/course/content
都是一片空白,检查元素,发现没有加载过来。检查,子路由前面并没有加/,所以这没有问题,排除。
其实这是list的父级course没有component,有了componnet,并且需要在这个component里面要有<router-view></router-view>