Detailed explanation of the use of slot sockets in vue components

php中世界最好的语言
Release: 2018-05-02 13:39:17
Original
1610 people have browsed it

This time I will bring you a detailed explanation of the use of slot sockets in vue components. What are theprecautionsfor using slot sockets in vue components. The following is a practical case, let's take a look.

Child component

  
Copy after login

Parent component

  
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

{{msg} }

will be discarded.

When slot has a default value

default value

, and the parent element is in ## When there is no content to be inserted in #,

default value

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 , 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: ` 

子组件

主体默认值

`, }; var parentNode = { template: `

父组件

我是主体

我是其他内容

我是尾部

`, components: { 'child': childNode }, };

插入中,

我是其他内容

插入中,而

被丢弃

Copy after login

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

var childNode = { template: ` 

子组件

主体默认值

`, }; var parentNode = { template: `

父组件

我是主体

我是其他内容

我是尾部

`, components: { 'child': childNode }, };

我是其他内容

都被抛弃

Copy after login

Scope slotA 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

Copy after login

In the parent, the

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: ` 
    默认值
`, data(){ return{ items:[ {id:1,text:'第1段'}, {id:2,text:'第2段'}, {id:3,text:'第3段'}, ] } } }; var parentNode = { template: `

父组件

`, 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!

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