Home > Web Front-end > JS Tutorial > body text

How to use vue+slot socket in the project

php中世界最好的语言
Release: 2018-06-11 15:23:10
Original
2106 people have browsed it

This time I will show you how to use the vue slot socket in the project, and what are the precautions for using the vue slot socket in the project. 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 kind The situation is that if you want the parent component to insert content into the child component, you must declare the slot tag in the child component. If the child component 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.

Named slot## element can be configured with a special attribute name 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

var childNode = {
 template: `
 <p class="child">
 <p>子组件</p>
 <slot name="my-header">头部默认值</slot>
 <slot name="my-body">主体默认值</slot>
 <slot name="my-footer">尾部默认值</slot>
 </p>
 `,
};
var parentNode = {
 template: `
 <p class="parent">
 <p>父组件</p>
 <child>
 <p slot="my-header">我是头部</p>
 <p slot="my-footer">我是尾部</p>
 </child>
 </p>
 `,
 components: {
 'child': childNode
 },
};
Copy after login

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