Vue路由 router-命名视图 leve4

Vue路由 router-命名视图 leve4

我们路由中的每一个router-view都是一个视图,我们的router-link会找离他最近的router-view,也可以理解为一个组件中的router-link会去找当前组件中的router-view。我们一个组件中如果有多个视图那么这个时候我们就需要用到命名视图了。

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="/node_modules/vue/dist/vue.js"></script>
    <script src="/node_modules/vue-router/dist/vue-router.js"></script>
    <style>
        .active{
            font-weight: 600;
            color: red;
        }
    </style>
</head>
<body>
    <div id="app">
        <ul>
            <router-link tag="li" to="/home" exact active-class="active">首页</router-link>
            <router-link tag="li" to="/search" exact active-class="active">搜索</router-link>
        </ul>
        <router-view name="header"></router-view>
        <router-view></router-view>
        <router-view name="footer"></router-view>
    </div>
    <script>
        const position = {
            template:`
                <div>position</div>
            `
        }
        const search = {
            template:`
                <div>search</div>
            `
        }
        /* 这里配置我们的路由 */
        let router = new VueRouter({
            routes:[
                {//配置默认路径
                    path:"/",
                    redirect:"/home"
                },
                {
                    path:'/home',
                    // 多视图
                    components:{
                        default:{template:'<div>main content</div>'},//默认视图 也就是说我们没有给router-view起名字时 它就叫default
                        header:{template:'<div>header content</div>'},
                        footer:{template:'<div>footer content</div>'}
                    },
                },
                {
                    path:'/search',
                    component:search,
                },
            ]
        })

        let vm = new Vue({
            router,
            el:'#app'
        })
    </script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值