Home  >  Article  >  Web Front-end  >  Detailed explanation of the use of slot sockets in vue components

Detailed explanation of the use of slot sockets in vue components

php中世界最好的语言
php中世界最好的语言Original
2018-04-08 14:04:061881browse

This time I bring to you, what are the precautions, the following is a practical case, let's take a look.

Child component



Parent component



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

var childNode = {
 template: `
 

 

子组件

 头部默认值  主体默认值  尾部默认值  

 `, }; var parentNode = {  template: `  

 

父组件

   

我是头部

 

我是尾部

 
 

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

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  }, };

插入中,

我是其他内容

插入中,而

被丢弃

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  }, };

我是其他内容

都被抛弃

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

 

In the parent, the