美文网首页
17. 插槽

17. 插槽

作者: 论宅 | 来源:发表于2019-06-01 23:15 被阅读0次

插槽内容

组件标签中间是可以塞内容进去的,而这个内容就是slot,插槽。只有启动了插槽才能显示标签内容——

<component1>slot内容{{message}}</component1>

var component1 = {
            template: `
            <div>
            听说插槽
            <slot>没有solt内容会显示这个</slot>
            会放在这上面哦    
            </div>
            `
        }
 var vue = new Vue({
            el: ".app",
            data: {
                message: "solt message"
            },
            components: {
                component1,
                component2
            }
        })

编译作用域

当你想在一个插槽内使用数据的时候,请注意拿到的数据是父元素的数据.例如上例中,拿到的message数据是从data中拿到的。

后背内容

模板中的<slot></slot>之间如果写内容的话,在slot没有内容的时候会显示出来——

var component1 = {
            template: `
            <div>
            听说插槽
            <slot>没有solt内容会显示这个</slot>
            会放在这上面哦    
            </div>
            `
        }

具名插槽

插槽不一定只有一个,一个组件里面可以有多个插槽,根据【名称】决定内容到底在哪里渲染:

 <div class="line"></div>
        <component2 v-slot:line1>内容</component2>
        <div class="line"></div>
        <component2 v-slot:line2>内容</component2>
        <div class="line"></div>
        <component2 v-slot:line3>内容</component2>
        <div class="line"></div>


var component2 = {
            template: `
            <div>
                <div>
                    title
                <slot name="line1"></slot>
                </div>

                <div>
                    content
                <slot name="line2"></slot>
                </div>

                <div>
                    bottom
                <slot name="line3" v-bind:title="title" >{{title}}</slot>
                </div>

            </div>
            `,
            data: function () {
                return {
                    title: "通过bind方法传递参数"
                }
            }
        }

如上,标签上使用v-slot:name指定插槽的名称,之后内容就会渲染在插槽中。

 <slot name="line2"></slot>

不带名称的插槽会带有【default】这个名字

作用域插槽

作用域插槽可以获取子组件的数据,从而渲染到对应的插槽上:

 <component4>
            <template v-slot:other="otherValue">
                {{ otherValue}}
            </template>
        </component4>

注意,作用域插槽需要用template标签包裹,其参数otherValue是other插槽绑定的属性——

 var component4 = {
            data: function () {
                return {
                    slotProps: "aaa",
                    otherP: "1234"
                }
            },
            template: `
            <div>
                <ul>
                    <slot :slotProps=slotProps ></slot> 
                    <slot name="other"   :otherValue=otherP></slot>
                </ul>
            </div>
            `
        }

上例中模板定义了两个插,一个默认的插槽,一个名为other的插槽,其中other还绑定了参数otherP,这个属性名为otherValue,可以在标签中进行调用。

独占默认插槽的缩写语法

只有slot中只有默认slot的时候,才不需要template标签:

<current-user v-slot:default="slotProps">
  {{ slotProps.user.firstName }}
</current-user>

甚至default都可以不要:

<current-user v-slot="slotProps">
  {{ slotProps.user.firstName }}
</current-user>

另外缩写和普通写法不能混用,只要出现第二个slot就请把他们恢复成原来的样子。

解构插槽

<component4>
            <template v-slot:other="otherValue">
                {{ otherValue}}
            </template>
        </component4>

如果输出一次值,你就会发现结果是这样的:

{ "otherValue": "1234" }

所以这玩儿意解构一下会更加简洁

<component4>
            <template v-slot:other="{otherValue}">
                {{ otherValue}}
            </template>
        </component4>

// 结果
1234
```

### 动态插槽名
```
<base-layout>
  <template v-slot:[dynamicSlotName]>
    ...
  </template>
</base-layout>
```
使用循环输出也不是梦了。

### 具名插槽的缩写
```
<current-user #default="{ user }">
  {{ user.firstName }}
</current-user>
```
#号代替了原来的v-slot:。

相关文章

  • 17. 插槽

    插槽内容 组件标签中间是可以塞内容进去的,而这个内容就是slot,插槽。只有启动了插槽才能显示标签内容—— 编译作...

  • vue----slot插槽

    插槽分类 匿名插槽 具名插槽 作用域插槽

  • vue中slot插槽的使用

    插槽的种类:1、默认插槽、具名插槽、作用域插槽、解构插槽、动态插槽几种。转原文:https://www.jians...

  • vue3中的插槽

    插槽 默认插槽 具名插槽,v-slot可以简写为# 动态插槽 #[dynamicSlotName] 作用域插槽(...

  • 2.插槽

    匿名插槽 具名插槽 作用域插槽

  • 深入理解vue中的slot与slot-scope(自 2.6.0

    单个插槽 | 默认插槽 | 匿名插槽首先是单个插槽,单个插槽是vue的官方叫法,但是其实也可以叫它默认插槽,或者与...

  • vue 插槽 slot

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

  • vue插槽

    默认插槽(没有名字的插槽) 具名插槽(带名字的插槽) 老版 2.6.0以前 新版 作用域插槽(父组件可以获取子组件...

  • Vue总结4-插槽,Vuex,VueRouter

    1.插槽 1.1匿名插槽 52-Vue组件-匿名插槽 ...

  • 组件化高级

    插槽 什么是插槽 生活中有很多地方都有用到插槽,比如电脑的USB插槽,插板上的电源插槽,目的是让我们原来的设备具备...

网友评论

      本文标题:17. 插槽

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