• 静态嵌套路由:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app">
    <router-link to="/green">green</router-link>
    <router-link to="/fruit">fruit</router-link>
<router-view></router-view>
</div>
<template id="green">
<div>蔬菜
    <router-link to="/green/organic">有机</router-link>
    <router-link to="/green/inorganic">无机</router-link>
    <router-view></router-view>
</div>
</template>
<template id="fruit">
<div>水果
    <router-link to="/fruit/sweet">甜的</router-link>
    <router-link to="/fruit/acid">酸的</router-link>
    <router-view></router-view>
</div>
</template>
</body>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script>
    //父路由 有蔬菜,水果,蔬菜里面有有机蔬菜,无机蔬菜 ,水果里面有甜的酸的
    let green={template:'#green'}
    let organic={template:"<div>有机蔬菜organic</div>"}
    let inorganic={template:"<div>无机蔬菜inorganic</div>"}
    let fruit={template:'#fruit'}
    let sweet={template:"<div>甜的</div>"}
    let acid={template:"<div>酸的</div>"}
    //路由路径映射表
    let routes=[
        //路由默认去的第一个组件
        {
            path:'',
            component:green
        },
        {
            path:"/green",
            component:green,
            children:[
                {
                    path:"",
                    component:organic
                },
                {
                    path:"organic",
                    component:organic
                },
                {
                    path:"inorganic",
                    component:inorganic
                }
            ]
        },
        {
            path:"/fruit",
            component:fruit,
            children:[
                {
                    path:"",
                    component:sweet
                },
                {
                    path:"sweet",
                    component:sweet
                },
                {
                    path:"acid",
                    component:acid
                }
            ]
 
        },
        //所有没有找到时候,去地址/green的组件
        {
            path:"*",
            redirect:'/green'
        }
 
    ]
    //实例化一个路由
    let router=new VueRouter({
        routes
    })
    let vm=new Vue({
        el:"#app",
        data:{
        },
        router
    })
</script>
</html> 

嵌套路由要通过子路由的方式实现,如果改成以下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app">
    <router-link to="/green">green</router-link>
    <router-link to="/fruit">fruit</router-link>
<router-view></router-view>
</div>
<template id="green">
<div>蔬菜
    <router-link to="/green/organic">有机</router-link>
    <router-link to="/green/inorganic">无机</router-link>
    <router-view></router-view>
</div>
</template>
<template id="fruit">
<div>水果
    <router-link to="/sweet">甜的</router-link>
    <router-link to="/acid">酸的</router-link>
    <router-view></router-view>
</div>
</template>
</body>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script>
    //父路由 有蔬菜,水果,蔬菜里面有有机蔬菜,无机蔬菜 ,水果里面有甜的酸的
    let green={template:'#green'}
    let organic={template:"<div>有机蔬菜organic</div>"}
    let inorganic={template:"<div>无机蔬菜inorganic</div>"}
    let fruit={template:'#fruit'}
    let sweet={template:"<div>甜的</div>"}
    let acid={template:"<div>酸的</div>"}
    //路由路径映射表
    let routes=[
        //路由默认去的第一个组件
        {
            path:'',
            component:green
        },
        {
            path:"/green",
            component:green,
            children:[
                {
                    path:"",
                    component:organic
                },
                {
                    path:"organic",
                    component:organic
                },
                {
                    path:"inorganic",
                    component:inorganic
                }
            ]
        },
        {
            path:"/fruit",
            component:fruit
 
        },
        {
            path:"/sweet",
            component:sweet
 
        }, 
        {
            path:"/acid",
            component:acid
 
        },
        //所有没有找到时候,去地址/green的组件
        {
            path:"*",
            redirect:'/green'
        }
 
    ]
    //实例化一个路由
    let router=new VueRouter({
        routes
    })
    let vm=new Vue({
        el:"#app",
        data:{
        },
        router
    })
</script>
</html>

首先没法用类似/fruit/sweet的路由跳转
用/sweet这样的路由,会把模板渲染到第一个router-view里

  • 把fruit里的两种类型变成动态参数(接收任意水果类型作为动态参数)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app">
    <router-link to="/green">green</router-link>
    <router-link to="/fruit">fruit</router-link>
<router-view></router-view>
</div>
<template id="green">
<div>蔬菜
    <router-link to="/green/organic">有机</router-link>
    <router-link to="/green/inorganic">无机</router-link>
    <router-view></router-view>
</div>
</template>
<template id="fruit">
<div>水果
    <router-link to="/fruit/sweet">甜的</router-link>
    <router-link to="/fruit/acid">酸的</router-link>
    <router-view></router-view>
</div>
</template>
</body>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script>
    //父路由 有蔬菜,水果,蔬菜里面有有机蔬菜,无机蔬菜 ,水果里面有甜的酸的
    let green={template:'#green'}
    let organic={template:"<div>有机蔬菜organic</div>"}
    let inorganic={template:"<div>无机蔬菜inorganic</div>"}
    let fruit={template:'#fruit'}
    let fruittype ={template:"<div>{{ $route.params.type }}</div>"}
    //let sweet={template:"<div>甜的</div>"}
    //let acid={template:"<div>酸的</div>"}
    //路由路径映射表
    let routes=[
        //路由默认去的第一个组件
        {
            path:'',
            component:green
        },
        {
            path:"/green",
            component:green,
            children:[
                {
                    path:"",
                    component:organic
                },
                {
                    path:"organic",
                    component:organic
                },
                {
                    path:"inorganic",
                    component:inorganic
                }
            ]
        },
        {
            path:"/fruit",
            component:fruit,
            children:[
                {
                    path: '/',
                    redirect: 'sweet'
                },
                {
                    path:":type",
                    component:fruittype
                }
      
            ]
 
        },
 
    ]
    //实例化一个路由
    let router=new VueRouter({
        routes
    })
    let vm=new Vue({
        el:"#app",
        data:{
        },
        router
    })
</script>
</html>

本站以现代、古代情诗为主,情诗网创办于2013年,以原创爱情诗歌、经典情诗、现代情诗、古代情诗、英文情诗、情诗绝句为主并收集古诗、古诗词、诗歌大全、诗词名句的文学门户。方便您下次继续阅读;可以放在浏览器的收藏夹中(快捷键Ctrl+D);或者看到喜欢或者有趣的诗词可以通过分享按钮给你的好友分享;情诗网是目前最全情诗大全网站之一。并欢迎广大诗歌爱好者阅览投稿!喜欢本站的话请大家把本站告诉给你朋友哦!地址是 www.qingshiwang.com !