Home Web Front-end JS Tutorial Detailed explanation of the use of slot sockets in vue components

Detailed explanation of the use of slot sockets in vue components

May 02, 2018 pm 01:39 PM
slot use Detailed explanation

This time I will bring you a detailed explanation of the use of slot sockets in vue components. What are the precautions for using slot sockets in vue components. 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 <slot> socket, the content of the parent component&lt;p&gt;{{msg} }&lt;/p&gt; will be discarded.

When slot has a default value <slot><p>default value</p></slot>, and the parent element is in &lt;slotshow&gt;&lt;/slotshow&gt;## When there is no content to be inserted in #, &lt;p&gt;default value&lt;/p&gt; 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 &lt;child&gt; , the value set in the parent component is displayed. The

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

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

var childNode = {
 template: `
 &lt;p class=&quot;child&quot;&gt;
 &lt;p&gt;子组件&lt;/p&gt;
 &lt;slot name=&quot;my-body&quot;&gt;主体默认值&lt;/slot&gt;
 &lt;/p&gt;
 `,
};
var parentNode = {
 template: `
 &lt;p class=&quot;parent&quot;&gt;
 &lt;p&gt;父组件&lt;/p&gt;
 &lt;child&gt;
 &lt;p slot=&quot;my-body&quot;&gt;我是主体&lt;/p&gt;
 &lt;p&gt;我是其他内容&lt;/p&gt;
 &lt;p slot=&quot;my-footer&quot;&gt;我是尾部&lt;/p&gt;
 &lt;/child&gt;
 &lt;/p&gt;
 `,
 components: {
 'child': childNode
 },
};
&lt;p&gt;我是其他内容&lt;/p&gt;和&lt;p slot=&quot;my-footer&quot;&gt;都被抛弃
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

&lt;span style=&quot;font-size: 16px&quot;&gt;&lt;p class=&quot;child&quot;&gt;
 &lt;slot text=&quot;hello from child&quot;&gt;&lt;/slot&gt;
&lt;/p&gt;&lt;/span&gt;
Copy after login

In the parent, the <template> element with the special attribute scope Must be present to indicate that it is a template for a scoped slot. The value of scope corresponds to a temporary

variable name

. This variable receives the props object passed from the child component.

If the above result is rendered, the output obtained is

[List GroupComponent]A more representative use case for scope slots is the list component , allowing components to customize how each item in the list should be rendered

var childNode = {
 template: `
 &lt;ul&gt;
 &lt;slot name=&quot;item&quot; v-for=&quot;item in items&quot; :text=&quot;item.text&quot;&gt;默认值&lt;/slot&gt;
 &lt;/ul&gt;
 `,
 data(){
 return{
 items:[
 {id:1,text:'第1段'},
 {id:2,text:'第2段'},
 {id:3,text:'第3段'},
 ]
 }
 }
};
var parentNode = {
 template: `
 &lt;p class=&quot;parent&quot;&gt;
 &lt;p&gt;父组件&lt;/p&gt;
 &lt;child&gt;
 &lt;template slot=&quot;item&quot; scope=&quot;props&quot;&gt;
 &lt;li&gt;{{ props.text }}&lt;/li&gt;
 &lt;/template&gt;
 &lt;/child&gt;
 &lt;/p&gt;
 `,
 components: {
 'child': childNode
 },
};
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting content, please pay attention to other related articles on the PHP Chinese website !

Recommended reading:

How Vue calls the third-party verification code


detailed introduction and use of vue filter


Detailed explanation of the 4 steps of vue cli upgrade webpack

The above is the detailed content of Detailed explanation of the use of slot sockets in vue components. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What software is crystaldiskmark? -How to use crystaldiskmark? What software is crystaldiskmark? -How to use crystaldiskmark? Mar 18, 2024 pm 02:58 PM

What software is crystaldiskmark? -How to use crystaldiskmark?

Detailed explanation of obtaining administrator rights in Win11 Detailed explanation of obtaining administrator rights in Win11 Mar 08, 2024 pm 03:06 PM

Detailed explanation of obtaining administrator rights in Win11

How to download foobar2000? -How to use foobar2000 How to download foobar2000? -How to use foobar2000 Mar 18, 2024 am 10:58 AM

How to download foobar2000? -How to use foobar2000

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

How to use NetEase Mailbox Master

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

How to use Baidu Netdisk app

Detailed explanation of division operation in Oracle SQL Detailed explanation of division operation in Oracle SQL Mar 10, 2024 am 09:51 AM

Detailed explanation of division operation in Oracle SQL

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange?

Teach you how to use the new advanced features of iOS 17.4 'Stolen Device Protection' Teach you how to use the new advanced features of iOS 17.4 'Stolen Device Protection' Mar 10, 2024 pm 04:34 PM

Teach you how to use the new advanced features of iOS 17.4 'Stolen Device Protection'

See all articles