插槽基本用法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <cpn></cpn>
        
        <cpn>
            <div>插槽</div>
        </cpn>
    </div>

    <template id="cpn">
        <div>
            <p>我是子组件</p>
            <slot></slot>
            <slot><button>我是默认值</button></slot> <!-- 插槽的默认值,有值则会覆盖 -->
        </div>
    </template>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        Vue.component('cpn',{
            template:'#cpn'
        });

        new Vue({
            el:'#app',
            data:{
                message:'hello world'
            }
        })
    </script>
</body>
</html>

 

 

posted @ 2022-02-27 21:26  Mr_sven  阅读(70)  评论(0编辑  收藏  举报