search
HomeWeb Front-endJS TutorialDetailed graphic explanation of the use of slot-scope in vue

This time I will bring you a detailed graphic explanation of the use of slot-scope in vue. What are the precautions when using slot-scope? Here are practical cases, let’s take a look.

Written in front

The document description about the slot in vue is very short, and the language is very concise, plus its and Differences in the frequency and order of use of common options such as methods, data, computed, etc. may cause developers who are new to slots to easily think "Forget it, learn it later, you can already write basic components anyway", so Just close the vue documentation.

In fact, the concept of slots is very simple. Let’s talk about it in 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. This template displays Whether or not to display and how to display it 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 slot, it is an empty shell, because its display and hiding and the final html template used to display 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 template passed from the parent component 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 it can also be called It is the default slot, or as opposed to the 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 the 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 names (name attribute) are different.

The following is an example.

Parent component:

<template>
 <p>
  </p>
<h3 id="这里是父组件">这里是父组件</h3>
  <child>
   <p>
    <span>菜单1</span>
    <span>菜单2</span>
    <span>菜单3</span>
    <span>菜单4</span>
    <span>菜单5</span>
    <span>菜单6</span>
   </p>
  </child>
 
</template>

Child component:

<template>
 <p>
  </p>
<h3 id="这里是子组件">这里是子组件</h3>
  <slot></slot>
 
</template>

In this example, because the parent component writes the html template in , Then the template for the anonymous slot of the subcomponent is as follows. In other words, the anonymous slot of the subcomponent is used by the template below.

<p>
 <span>菜单1</span>
 <span>菜单2</span>
 <span>菜单3</span>
 <span>菜单4</span>
 <span>菜单5</span>
 <span>菜单6</span>
</p>

The final rendering result is as shown in the figure:

Note: All demos have been styled for easier observation. 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. The following 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>
 </p>
<h3 id="这里是父组件">这里是父组件</h3>
 <child>
  <p>
  <span>菜单1</span>
  <span>菜单2</span>
  <span>菜单3</span>
  <span>菜单4</span>
  <span>菜单5</span>
  <span>菜单6</span>
  </p>
  <p>
  <span>菜单-1</span>
  <span>菜单-2</span>
  <span>菜单-3</span>
  <span>菜单-4</span>
  <span>菜单-5</span>
  <span>菜单-6</span>
  </p>
  <p>
  <span>菜单->1</span>
  <span>菜单->2</span>
  <span>菜单->3</span>
  <span>菜单->4</span>
  <span>菜单->5</span>
  <span>菜单->6</span>
  </p>
 </child>
 
</template>

Child component:

<template>
 <p>
 // 具名插槽
 <slot></slot>
 </p>
<h3 id="这里是子组件">这里是子组件</h3>
 // 具名插槽
 <slot></slot>
 // 匿名插槽
 <slot></slot>
 
</template>

The display result is as shown in the figure:

You can see, The parent component associates 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 written in the template of the component.

Anonymous slot

<slot></slot>

Named slot

<slot></slot>

But the scope slot requires, Bind data to the slot. That is to say, you have to write it roughly like the following.

<slot></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>
 </p>
<h3 id="这里是父组件">这里是父组件</h3>
 <!--第一次使用:用flex展示数据-->
 <child>
  <template>
  <p>
   <span>{{item}}</span>
  </p>
  </template>
 </child>
 <!--第二次使用:用列表展示数据-->
 <child>
  <template>
  <ul>
   <li>{{item}}</li>
  </ul>
  </template>
 </child>
 <!--第三次使用:直接显示数据-->
 <child>
  <template>
  {{user.data}}
  </template>
 </child>
 <!--第四次使用:不使用其提供的数据, 作用域插槽退变成匿名插槽-->
 <child>
  我就是模板
 </child>
 
</template>

子组件:


 export default {
 data: function(){
  return {
  data: ['zhangsan','lisi','wanwu','zhaoliu','tianqi','xiaoba']
  }
 }
}

结果如图所示:

github

以上三个demo就放在GitHub了,有需要的可以去取。使用非常方便,是基于vue-cli搭建工程。

地址点这里

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

js动态操作表格

用name取select值

微信小程序的多文件下载封装使用

JS中Object对象的原型的使用方法

vue.js实现单次弹框

The above is the detailed content of Detailed graphic explanation of the use of slot-scope in vue. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools