Home > Web Front-end > JS Tutorial > Detailed explanation on the use of slot socket of vue component

Detailed explanation on the use of slot socket of vue component

php中世界最好的语言
Release: 2018-04-20 13:37:32
Original
1456 people have browsed it

This time I will bring you a detailed explanation of the use of the slot socket of the vue component. What are the precautions when using the slot socket of the vue component. The following is a practical case, let's take a look.

Child component

<template>
  <p class="slotcontent">
    <ul>
      <!--<slot></slot>-->
      <li v-for="item in items">{{item.text}}</li>
    </ul>
  </p>
</template>
<script>
  export default{
    data(){
      return{
        items:[
          {id:1,text:'第1段'},
          {id:2,text:'第2段'},
          {id:3,text:'第3段'},
        ]
      }
    }
  }
</script>
<style scoped>
</style>
Copy after login

Parent component

<template>
  <p>
    <h2>首页</h2>
    <router-link to="/home/details">跳转到详情</router-link>
    <p>父组件</p>
    <slotshow>
      <p>{{msg}}</p>
    </slotshow>
  </p>
</template>
<script>
  import slotshow from '../components/slotshow'
  export default{
    data(){
      return{
        msg:"测试内容"
      }
    },
    components:{
      slotshow
    }
  }
</script>
<style>
</style>
Copy after login

This situation is if you want a parent component To insert content into a subcomponent, the slot tag must be declared in the subcomponent. If the subcomponent template does not contain the socket, the content of the parent component<p>{{msg}}</p> will be discarded.

When slot has a default value

default value

, and the parent element is in <slotshow></slotshow>## When there is no content to be inserted in #, <p>default value</p> will be displayed (the p tag will be removed). When the slot has a default value and the parent element has content to be inserted in <child> , the value set in the parent component is displayed. The

named slot## element can use a special

attribute

name to configure how to distribute content. Multiple slots can have different names. The named slot will match elements with the corresponding slot attribute in the content fragment

You can still have an anonymous slot, which is the default slot, as if no matching content fragment is found Spare slot. Anonymous slots can only be used as slots for elements without slot attributes. Elements with slot attributes will be discarded if no slot is configured.

var childNode = {
 template: `
 <p class="child">
 <p>子组件</p>
 <slot name="my-body">主体默认值</slot>
 <slot></slot>
 </p>
 `,
};
var parentNode = {
 template: `
 <p class="parent">
 <p>父组件</p>
 <child>
 <p slot="my-body">我是主体</p>
 <p>我是其他内容</p>
 <p slot="my-footer">我是尾部</p>
 </child>
 </p>
 `,
 components: {
 'child': childNode
 },
};
<p slot="my-body">插入<slot name="my-body">中,<p>我是其他内容</p>插入<slot>中,而<p slot="my-footer">被丢弃
Copy after login

If there is no default slot, These content fragments that cannot find a match will also be discarded

var childNode = {
 template: `
 <p class="child">
 <p>子组件</p>
 <slot name="my-body">主体默认值</slot>
 </p>
 `,
};
var parentNode = {
 template: `
 <p class="parent">
 <p>父组件</p>
 <child>
 <p slot="my-body">我是主体</p>
 <p>我是其他内容</p>
 <p slot="my-footer">我是尾部</p>
 </child>
 </p>
 `,
 components: {
 'child': childNode
 },
};
<p>我是其他内容</p>和<p slot="my-footer">都被抛弃
Copy after login

Scope slot A scoped slot is a special type of slot used to replace a rendered element with a reusable template that can pass data to it.

In the child component, just pass the data to the slot, just like passing props to the component

<span style="font-size: 16px"><p class="child">
 <slot text="hello from child"></slot>
</p></span>
Copy after login

In the parent, the