内容分发-作用域插槽传参解构

<!DOCTYPE html>
<html lang="zh">

<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>
</head>

<body>

    <div id="app">

        <header-component>
            <template v-slot:slot1="{ msg1: m1, msg2: m2 }">
               <h1>{{rootMsg}}</h1>
               <h1>{{m1}}</h1>
               <h1>{{m2}}</h1>
            </template>
        </header-component>

    </div>



    <script src="./vue.js"></script>
    <script>

        new Vue({
            el: '#app',
            data: {
                rootMsg: '根组件的数据'
            },
            components: {
                'header-component': {
                    data () {
                        return {
                            childMsg1: '子组件的数据1',
                            childMsg2: '子组件的数据2'
                        }
                    },
                    template: `<div><slot name="slot1" :msg1="childMsg1" :msg2="childMsg2"></slot></div>`
                }
            }
        })

        // msg1 和 msg2 的值会被放到一个对象中,然后这个对象会被赋值给 args 参数
        // const args = {
        //     msg1: '子组件的数据1',
        //     msg2: '子组件的数据2'
        // }

        // 使用对象解构接收参数
        const { msg1 } =  {
            msg1: '子组件的数据1',
            msg2: '子组件的数据2'
        }
    </script>
</body>

</html>
发布了151 篇原创文章 · 获赞 1 · 访问量 1855

猜你喜欢

转载自blog.csdn.net/qq_45802159/article/details/103829520