


Detailed explanation of the steps for using slot and slot-scope in vue
This time I will bring you a detailed explanation of the steps for using slot and slot-scope in vue. What are the precautions for using slot and slot-scope in vue. The following is a practical case. Get up and take a look.
This article introduces the specific usage tutorial of slot and slot-scope in vue. Friends in need come and take a look
Write in front
The document description about slots in vue is very short, and the language is very concise. In addition, it is different from the frequency and order of use of common options such as methods, data, computed, etc., this is possible causing the first Developers who come into contact with slots are prone to "Forget it, learn it later, you can already write basic components anyway", so they close the Vue documentation.
In fact, the concept of slots is very simple, and the following is divided into three parts. This part is also written in the order of the vue documentation.
Before entering the third part, let students who have not been exposed to slots have a simple concept of what a slot is: a slot, also known as a slot, is an HTML template of a component. Whether this template is displayed, and how to display it The display is determined by the parent component. In fact, the two core issues of a slot are highlighted here, which are whether to display it and how to display it.
Since the slot is a template, any component can actually be divided into two categories: non-slot templates and slot templates from the perspective of template type.
Non-slot templates refer to html templates, which refer to 'p, span, ul, table', etc. The display and hiding of non-slot templates and how to display them are controlled by the plug-in itself; the slot template is a slot, which is an empty space. Shell, because its display and hiding and what kind of html template is finally displayed are controlled by the parent component. However, the position of the slot display is indeed determined by the sub-component itself. Where the slot is written in the component template, the parent component passes it. The template will be displayed in the future.
Single slot | Default slot | Anonymous slot
The first is a single slot. A single slot is the official name of Vue, but in fact it can also be called a default slot, or as opposed to a named slot, we can call it an anonymous slot. Because it does not need to set the name attribute.
A single slot can be placed anywhere in a component, but as its name suggests, there can only be one slot of this type in a component. Correspondingly, there can be many named slots, as long as the name (name attribute sex) is different.
This is demonstrated below with an example.
Parent component:
<template> <p class="father"> <h3>这里是父组件</h3> <child> <p class="tmpl"> <span>菜单1</span> <span>菜单2</span> <span>菜单3</span> <span>菜单4</span> <span>菜单5</span> <span>菜单6</span> </p> </child> </p> </template>
Subcomponent:
<template> <p class="child"> <h3>这里是子组件</h3> <slot></slot> </p> </template>
In this example, because the parent component has an HTML template written in
<p class="tmpl"> <span>菜单1</span> <span>菜单2</span> <span>菜单3</span> <span>菜单4</span> <span>菜单5</span> <span>菜单6</span> </p>
Note: All demos have been styled for easier viewing. Among them, the parent component is filled with a gray background, and the child components are filled with a light blue background.
Named slot
The anonymous slot does not have a name attribute, so it is an anonymous slot. Then, if the name attribute is added to the slot, it becomes a named slot. A named slot can appear N times in a component. Appear in different locations. below An example is a component with two named slots and a single slot. These three slots are displayed by the parent component using the same set of CSS styles, but the content is slightly different.
Parent component:
<template> <p class="father"> <h3>这里是父组件</h3> <child> <p class="tmpl" slot="up"> <span>菜单1</span> <span>菜单2</span> <span>菜单3</span> <span>菜单4</span> <span>菜单5</span> <span>菜单6</span> </p> <p class="tmpl" slot="down"> <span>菜单-1</span> <span>菜单-2</span> <span>菜单-3</span> <span>菜单-4</span> <span>菜单-5</span> <span>菜单-6</span> </p> <p class="tmpl"> <span>菜单->1</span> <span>菜单->2</span> <span>菜单->3</span> <span>菜单->4</span> <span>菜单->5</span> <span>菜单->6</span> </p> </child> </p> </template>
Subcomponent:
<template> <p class="child"> // 具名插槽 <slot name="up"></slot> <h3>这里是子组件</h3> // 具名插槽 <slot name="down"></slot> // 匿名插槽 <slot></slot> </p> </template>
As you can see, the parent component is associated with the named slot through the slot attribute on the html template. HTML templates without slot attributes are associated with anonymous slots by default.
Scope slot | slot with data
Finally, there is our scope slot. This one is a little harder to understand. Officially, it is called a scope slot. In fact, compared with the previous two slots, we can call it a slot with data. What does it mean? The first two are to write
in the template of the component. Anonymous Slots
<slot></slot>
Named Slot
<slot name="up"></slot>
However, the scope slot requires binding data on the slot. That is to say, you have to write it roughly like the following.
<slot name="up" :data="data"></slot> export default { data: function(){ return { data: ['zhangsan','lisi','wanwu','zhaoliu','tianqi','xiaoba'] } }, }
我们前面说了,插槽最后显示不显示是看父组件有没有在child下面写模板,像下面那样。
<child> html模板 </child>
写了,插槽就总得在浏览器上显示点东西,东西就是html该有的模样,没写,插槽就是空壳子,啥都没有。
OK,我们说有html模板的情况,就是父组件会往子组件插模板的情况,那到底插一套什么样的样式呢,这由父组件的html+css共同决定,但是这套样式里面的内容呢
?
正因为作用域插槽绑定了一套数据,父组件可以拿来用。于是,情况就变成了这样:样式父组件说了算,但内容可以显示子组件插槽绑定的。
我们再来对比,作用域插槽和单个插槽和具名插槽的区别,因为单个插槽和具名插槽不绑定数据,所以父组件是提供的模板要既包括样式由包括内容的,上 面的例子中,你看到的文字,“菜单1”,“菜单2”都是父组件自己提供的内容;而作用域插槽,父组件只需要提供一套样式(在确实用作用域插槽绑定的数据的前 提下)。
下面的例子,你就能看到,父组件提供了三种样式(分别是flex、ul、直接显示),都没有提供数据,数据使用的都是子组件插槽自己绑定的那个人名数组。
父组件:
<template> <p class="father"> <h3>这里是父组件</h3> <!--第一次使用:用flex展示数据--> <child> <template slot-scope="user"> <p class="tmpl"> <span v-for="item in user.data">{{item}}</span> </p> </template> </child> <!--第二次使用:用列表展示数据--> <child> <template slot-scope="user"> <ul> <li v-for="item in user.data">{{item}}</li> </ul> </template> </child> <!--第三次使用:直接显示数据--> <child> <template slot-scope="user"> {{user.data}} </template> </child> <!--第四次使用:不使用其提供的数据, 作用域插槽退变成匿名插槽--> <child> 我就是模板 </child> </p> </template>
子组件:
<template> <p class="child"> <h3>这里是子组件</h3> // 作用域插槽 <slot :data="data"></slot> </p> </template> export default { data: function(){ return { data: ['zhangsan','lisi','wanwu','zhaoliu','tianqi','xiaoba'] } } }
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of Detailed explanation of the steps for using slot and slot-scope in vue. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of the mode function in C++ In statistics, the mode refers to the value that appears most frequently in a set of data. In C++ language, we can find the mode in any set of data by writing a mode function. The mode function can be implemented in many different ways, two of the commonly used methods will be introduced in detail below. The first method is to use a hash table to count the number of occurrences of each number. First, we need to define a hash table with each number as the key and the number of occurrences as the value. Then, for a given data set, we run

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

Detailed explanation of the remainder function in C++ In C++, the remainder operator (%) is used to calculate the remainder of the division of two numbers. It is a binary operator whose operands can be any integer type (including char, short, int, long, etc.) or a floating-point number type (such as float, double). The remainder operator returns a result with the same sign as the dividend. For example, for the remainder operation of integers, we can use the following code to implement: inta=10;intb=3;

Detailed explanation of the usage of Vue.nextTick function and its application in asynchronous updates. In Vue development, we often encounter situations where data needs to be updated asynchronously. For example, data needs to be updated immediately after modifying the DOM or related operations need to be performed immediately after the data is updated. The .nextTick function provided by Vue emerged to solve this type of problem. This article will introduce the usage of the Vue.nextTick function in detail, and combine it with code examples to illustrate its application in asynchronous updates. 1. Vue.nex

PHP-FPM is a commonly used PHP process manager used to provide better PHP performance and stability. However, in a high-load environment, the default configuration of PHP-FPM may not meet the needs, so we need to tune it. This article will introduce the tuning method of PHP-FPM in detail and give some code examples. 1. Increase the number of processes. By default, PHP-FPM only starts a small number of processes to handle requests. In a high-load environment, we can improve the concurrency of PHP-FPM by increasing the number of processes

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions
