Vue路由嵌套和命名视图

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>嵌套router</title>
    <script src="./lib/vue-2.4.0.js"></script>
    <script src="./lib/vue-router-3.0.1.js"></script>
</head>
<body>
    
    <div class="container">
        <!-- 符组件的连接指向了denglu路径 -->
        <router-link to="/denglu">显示</router-link>    
        <router-view></router-view>
    </div>


    <template id="account">
        <div>
            <h1>这个是一个组件</h1>
            <!-- 子组件的连接要指向denglu路径下的longin路径 -->
            <router-link to="/denglu/login">登录</router-link>
            <router-link to="/denglu/zhuce">注册</router-link>
            <router-view></router-view>
        </div>
    </template>

    <script>
        var box={
            template:'#account'
        }
        
        var login={
            template:'<h1>登录</h1>'
        }

        var zhuce={
            template:"<h1>注册</h1>"
        }
        var router=new VueRouter({
            routes: [
                // 路由规则主路径 指向了denglu    子路径使用children 属性,
                //实现子路由,同时,子路由的 path 前面,不要带 / ,否则永远以根路径开始请求,这样不方便我们用户去理解URL地址
                {path:'/denglu',component:box,children:[
                    {path:"login",component:login},
                    {path:"zhuce",component:zhuce},
                ]},
            ]
        })


        var vm=new Vue({
            el:".container",
            data:{},
            router
        })
    </script>
</body>
</html>

命名视图(就是给router-view  添加一个name属性)

  

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./lib/vue-2.4.0.js"></script>
    <script src="./lib/vue-router-3.0.1.js"></script>
</head>
<body>
    <div class="container">
        <router-view name="header"></router-view>
        <div class="aa">
            <router-view name="left"></router-view>
            <router-view name="mian"></router-view>
        </div>

    </div>


    <!-- 添加样式 -->
    <style>
        .box{
            padding: 0;
            margin: 0;
        }
        h1{
            padding: 0;
            margin: 0;
        }
        .headerbox{
            background-color: green;
        }
    .aa{
        display: flex;
        height: 600px;
    }
    
    .leftbox{
        flex:2;
        background-color: hotpink;
    }

    .mianbox{
        flex:8;
        background-color: indigo;
    }


    </style>







    <script>
    
    var heaer={
        template:"<h1 class='headerbox'>这是网页的头部</h1>"
    }

    var leftbox={
        template:"<h1 class='leftbox'>这是网页的侧边栏</h1>"
    }

    var mianbox={
        template:"<h1 class='mianbox'>这是网页的主体</h1>"
    }

    var router=new VueRouter({
        routes: [
            {path:'/',components:{
                header:heaer,
                left:leftbox,
                mian:mianbox
            }}
        ]
    })

    var vm=new Vue({
        el:".container",
        data:{},
        router:router
    })


    </script>
</body>
</html>
原文地址:https://www.cnblogs.com/xiaowie/p/11750460.html