美文网首页
在Vue中如何使用插槽

在Vue中如何使用插槽

作者: 云凡的云凡 | 来源:发表于2020-10-12 22:20 被阅读0次

很重要,很多第三方的Vue的插件或者模块中都大量的使用了插槽这种特性。
使用场景:Vue父组件向子组件传递dom结构,有两种方法:

1.通过属性传递,缺点:会多出一个div,template 不好用,所以通过content传值,直接使用p标签是有问题的,必须包一个div。传递少还行,当传递很多的时候,代码会很难去阅读。

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>16在Vue中如何使用插槽(slot)</title>
    <script src="./vue.js"></script>
</head>

<body>
    <div id="app">
        <child content='<p>哈哈哈</p>'></child>
    </div>
    <script>
        Vue.component('child', {
            props: [
                'content'
            ],
            template: '<div ><p>hello</p><div v-html="this.content"></div></div>'
        })
        var vm = new Vue({
            el: '#app'
        })
    </script>
</body>
</html>

2.使用slot可以在父组件向子组件优雅的传递dom结构

image.png
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>16在Vue中如何使用插槽(slot)</title>
    <script src="./vue.js"></script>
</head>
<body>
    <div id="app">
        <child>
            <!-- 1.用子组件时插入一点内容 -->
            <h1>hahaha</h1>
        </child>
    </div>
    <script>
        Vue.component('child', {
            // 2.用<slot></slot>可以显示  <p>hahaha</p>
            // 所以使用插槽会更方便的向子组件插入dom元素
            template: `<div>
                <p>hello</p>
                <slot></slot>
                </div>`
        })
        var vm = new Vue({
            el: '#app'
        })
    </script>
</body>
</html>

slot有新的特性
1.定义默认值,定义在slot标签上
2.具名插槽

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>16在Vue中如何使用插槽(slot)</title>
    <script src="./vue.js"></script>
</head>
<body>
    <div id="app">
        <child>
            <div class="footer" slot="footer">footer</div>
        </child>
    </div>
    <script>
        Vue.component('child', {
            template: `<div>
                <slot name="header"></slot>
                <div class="content">content</div>
                <slot name="footer"></slot>
                </div>`
        })
        var vm = new Vue({
            el: '#app'
        })
    </script>
</body>
</html>

总结:插槽只有一个,而具名插槽有多个,同时具名插槽可以有默认值。。
不传的时候会使用默认值

image.png

相关文章

  • 在Vue中如何使用插槽

    很重要,很多第三方的Vue的插件或者模块中都大量的使用了插槽这种特性。使用场景:Vue父组件向子组件传递dom结构...

  • 2020-07-23 一次性讲明白vue插槽slot

    vue插槽slot 一、前言 vue官方文档中在"组件基础"内容中提到组件可以通过插槽分发内容,那插槽是怎么使用的...

  • vue插槽slot

    vue插槽slot 一、前言 vue官方文档中在"组件基础"内容中提到组件可以通过插槽分发内容,那插槽是怎么使用的...

  • 18、Vue3 作用域插槽

    作用域插槽:让插槽内容能够访问子组件中,vue2中作用域插槽使用slot-scope,vue3中使用v-slot ...

  • (十八)补充-Vue3中插槽的使用

    本章我们将了解到的是vue3中常用插槽的使用; vue3中的插槽是在用的时候和vue2的区别略有不同,常见插槽使用...

  • 详解vue中的插槽

    1.在vue中插槽分为具名插槽和非具名插槽;而插槽的使用主要是我们在页面中存在很多个相似但却重复的部分; 首先我以...

  • vue插槽

    vue插槽slot的理解与使用 vue slot插槽的使用介绍及总结

  • vue 插槽的使用

    vue 插槽手册 深入理解vue中的slot与slot-scope 插槽的使用其实是很简单首先要明白插槽是使用在子...

  • 在vue中插槽的使用

  • vue 插槽 slot

    插槽使用 普通插槽 具名插槽 使用具名插槽 从插槽里面传值出来如何接收? 如: 如何判断某个插槽是否被使用 组件内...

网友评论

      本文标题:在Vue中如何使用插槽

      本文链接:https://www.haomeiwen.com/subject/grnrpktx.html