Home > Article > Web Front-end > Detailed explanation of the use of slot sockets in vue components
This time I bring to you, what are the precautions, the following is a practical case, let's take a look.
Child component
- {{item.text}}
Parent component
首页
跳转到详情 父组件
{{msg}}
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 {{msg} }
will be discarded.
When slot has a default value default value default value
named slot##
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: `
父组件
我是头部
我是尾部
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 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.
子组件var childNode = {
template: `
父组件
hello from parent
{{ props.xxx }}
[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: `
父组件
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:
React realizes data synchronization of mobile phone numbersHow to use node server for cross-domain development in local developmentvue-router adds loading prompts when lazy loading to improve user experienceThe 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!