美文网首页
插槽的使用

插槽的使用

作者: 方_糖 | 来源:发表于2021-05-21 17:27 被阅读0次
1. 插槽和组件的区别

插槽和组件的用法基本相同,主要的区别是父组件可以在插槽中自定义子组件的DOM

2.基本使用
  • 子组件child.vue
<template>
    <div>
        <span>今天天气状况:</span>
        <slot>暂无</slot>    <!--“暂无”是当父组件没传值时显示的默认内容-->
    </div>
</template>
<script>
    export default {
        name: 'child'
    }
</script>
  • 父组件
<template>
    <div>
        <child>{{type}}</child>
    </div>
</template>

<script>
import child from "./child"
export default {
    name: "slot-x",
    data(){
        return {
            type:"大风"
        }
    },
    components: {
       'child': child
    }
}
</script>
3. 具名插槽

现在组件child中只能插入一条内容,如果想在不同地方插入多条怎么办呢?这时可以使用<slot>元素的name属性

  • 子组件child.vue
<template>
    <div>
        <span>今天天气状况:</span>
        <slot name="type"></slot>  
        <slot name="level"></slot>  
    </div>
</template>
  • 父组件
<template>
    <div>
        <child>
            <span slot="type">{{type}}</span>
            <span slot="level">八级</span>
        </child>
    </div>
</template>

使用template包裹插槽内容

  • 父组件
<template>
    <div>
        <child>
            <template slot="type">
                <span>{{type}}</span>
                <span>小雨</span>
            </template>
            <template slot="level">
                <span>八级</span>
            </template>
        </child>
    </div>
</template>
4. 作用域插槽

作用:让插槽内容能够访问子组件中才有的数据

  • 子组件child.vue
<template>
    <div>
        <span>今天天气状况:</span>
        <slot name="level" :level="childLevel"></slot>  
    </div>
</template>
<script>
    export default {
        name: 'child',
        data() {
            return {
                childLevel: "四级"
            }
        }
    }
</script>
  • 父组件
<template>
    <div>
        <child>
            <template slot="level" slot-scope="{level}">
                <span>{{level}}</span>
            </template>
        </child>
    </div>
</template>

<script>
import child from "./child"
export default {
    name: "slot-x",
    components: {
       'child': child
    }
}
</script>

注意:v-slot 指令自 Vue 2.6.0 起被引入,提供更好的支持 slotslot-scope attribute 的 API 替代方案。v-slot 完整的由来参见这份 RFC。在接下来所有的 2.x 版本中 slotslot-scope attribute 仍会被支持,但已经被官方废弃且不会出现在 Vue 3 中。

相关文章

  • vue 插槽 slot

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

  • 【Vue8】插槽

    插槽很重要!插件和模块中会经常使用插槽。 插槽的使用场景 即什么时候使用插槽?比如说这样: 我是插槽slot 父组...

  • vue 插槽slot

    插槽的定义: 插槽的使用:

  • Vue 插槽

    1.插槽的简单使用 在组件中声明一个插槽 使用组件时 可以替换插槽 如果不替换插槽就使用默认值 2.具名...

  • vue插槽

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

  • 【前端Vue】05 - [插槽 ,前端模块化开发,webpack

    1. 插槽 1.1 插槽的基本使用 组件的插槽: 组件的插槽也是为了让我们封装的组件更加具有扩展性。 让使用者可以...

  • Vue学习笔记[12]-slot插槽和编译作用域

    为了使组件具有更多的扩展性,需要使用插槽,使用插槽的组件应为可复用的组件,若为不复用组件,则不应该使用插槽. 使用...

  • vue插槽

    vue插槽使用 1.基本插槽实现父级设置 子级设置 2、使用name设置插槽显示隐藏父组件使用v-slot绑定 子...

  • Vue-使用插槽

    二。具名卡槽 作用域插槽:插槽循环时使用

  • Vue插槽的使用方法

    About 使用插槽是为了能够在页面中显示向组件中传入的新的HTML 一、具名插槽 效果图: 2. 具名插槽的使用...

网友评论

      本文标题:插槽的使用

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