Home > Web Front-end > HTML Tutorial > Why are slots used in subcomponents?

Why are slots used in subcomponents?

php中世界最好的语言
Release: 2018-02-24 09:37:42
Original
1985 people have browsed it

This time I will bring you why slots are used in sub-components, and what are the precautions for using slot sub-components. The following is a practical case, let’s take a look.

Use slot scenario 1:

Sub component Minput.vue

<input type=&#39;text&#39;/>
Copy after login

Parent component Minput

<Minput>可以显示吗</Minput>
Copy after login

In this case, the text in the Minput tag is not It will be rendered

What if I want to render the text inside now

Easy to use slot

Subcomponent

<input type=&#39;text&#39;/>
Copy after login

In this case, the parent component The text inside can be rendered

Scenario 2: Named slot

Subcomponent he.vue

<header>
    <slot name=&#39;header&#39;></slot>
</header>
Copy after login

Parent component

<he>
    <h1 name=&#39;header&#39;>hello world</h1>
</he>
Copy after login

Rendered The result is

<header><h1>hello world</h1></header>
Copy after login

Scenario 3

Child component child

<div>
    <h1>这是h1</h1>
    <slot>这是分发内容,只有在没有分发内容的情况下显示</slot>
</div>
Copy after login

Parent component

<child>
    <p>这是一段p</p>
    <p>两段p</p>
</child>
Copy after login

Rendered is

<div><h1>这是h1</h1><p>这是一段p</p><p>两段p</p></div>
Copy after login

If parent component

<child></child>
Copy after login

Then what is rendered is

<div><h1>这是h1</h1>这是分发内容,只有在没有分发内容的情况下显示</div>
Copy after login

Scenario 4: Scope slot

<div class="child">
  <slot text="hello from child"></slot>
</div>
Copy after login

Parent component

<div class="parent">
  <child>
    <template slot-scope="props">
      <span>hello from parent</span>
      <span>{{ props.text }}</span>
    </template>
  </child>
</div>
Copy after login

xIf rendered, it is

<div class="parent">
  <div class="child">
    <span>hello from parent</span>
    <span>hello from child</span>
  </div>
</div>
Copy after login

I believe you have mastered the methods after reading these cases. For more exciting information, please pay attention to other related articles on the php Chinese website!

Related reading:

How to make a "dot" border appear after clicking the button

Detailed explanation of the browser rendering process

How to make the input text box and img verification code

Commonly used input text box content is automatically vertically centered and defaults The prompt text click is empty

The above is the detailed content of Why are slots used in subcomponents?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template