Vue插槽slot的使用


vue官方文档:https://cn.vuejs.org/v2/guide/components-slots.html

<!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>
</head>
<body>
    <div id="app">
        <one>
            <h1 slot="header">333333</h1>
        </one>
    </div>
</body>
</html>
<template id="one">
    <div>
        <slot name="header">默认内容2222</slot>      
    </div>
</template>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/3.1.1/vue.cjs.js"></script>
<script>
    Vue.component('one', {
        template: '#one',
    })
    var vue = new Vue({
        el: '#app',
    })
</script>

父组件

<template>
    <div class="about">
        <h1>This is an about page</h1>
        <hr>
        <aboutCom ref="aboutCom">
            <template v-slot:header>
                <h1></h1>
            </template>
        </aboutCom>
    </div>
</template>
<script>
import aboutCom from "../components/aboutCom.vue";
import aboutComChild from "../components/aboutComChild.vue";
export default {
    components:{
        aboutCom,
        aboutComChild
    },
}
</script>

子组件

<template>
    <div>
       <h2>这是about的子组件</h2>
        <slot name="header">默认内容222222222</slot>
        <hr>
       <aboutComChild ref="aboutComChild"></aboutComChild>
    </div>
</template>
<script>
import aboutComChild from "./aboutComChild.vue";
export default {
    components:{
        aboutComChild
    },
}
</script>

文章作者: 弈心
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 弈心 !
评论
  目录