Table of Contents
What is a slot
具名作用域插槽
写在最后
Home Web Front-end Vue.js What is a slot? Let's talk about how to use slots in Vue3

What is a slot? Let's talk about how to use slots in Vue3

Aug 23, 2022 pm 07:57 PM
vue vue3 slot slot

What is a slot? Let's talk about how to use slots in Vue3

Vue I believe that everyone who has used Vue has used the slots more or less, but do you know all its uses? This article will bring you all the usage of slots in Vue3 to help you find and fill gaps. (Learning video sharing: vue video tutorial)

What is a slot

Simply speaking, it is the provided in the sub-component A pit used by the parent component, represented by <slot></slot>. The parent component can fill in any template code in this pit and then ## in the child component #<slot></slot> will be replaced with these contents. For example, in the simplest slot example

//父组件
<template>
  <div>
    <child>Hello Juejin</child>
  </div>
</template>
<script>
import Child from &#39;./Child.vue&#39;
</script>

//子组件Child
<template>
    <div>
        <p>1</p>
        <slot></slot>
        <p>2</p>
    </div>
</template>
Copy after login
, the

<slot></slot> in the child component is the parent component placed between the child component tag the content of the space. Of course, you can pass in any code snippet during this period, and it will be placed in the <slot></slot> position.

What is a slot? Lets talk about how to use slots in Vue3##Similarly, you can also put variables between the tags

, such as <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">//父组件 &lt;template&gt;   &lt;div&gt;     &lt;child&gt;{{ msg }}&lt;/child&gt;   &lt;/div&gt; &lt;/template&gt; &lt;script&gt; import { ref } from &amp;#39;vue&amp;#39; import Child from &amp;#39;./Child.vue&amp;#39; const msg = ref(&amp;#39;Hello Juejin&amp;#39;) &lt;/script&gt;</pre><div class="contentsignin">Copy after login</div></div> Let me explain the following first The two words that appear frequently

slot

and slot content prevent confusion in subsequent readings:

What is a slot? Lets talk about how to use slots in Vue3Same

Slot

represents this msg variable. Therefore, the child component slot can access the data scope of the parent component, while slot content cannot access the data of the child component (that is, the two &lt in the parent component Data in child components cannot be used between ;Child>), this is the so-called rendering scope. The method of slot passing parameters to slot content will be introduced later

Default content

is not provided in the parent component When any

slot content

is specified, we can specify the default content for the slot of the child component, such as <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">//子组件 &lt;template&gt;     &lt;div&gt;         &lt;slot&gt;我是默认内容&lt;/slot&gt;     &lt;/div&gt; &lt;/template&gt; //父组件1 &lt;template&gt;   &lt;div&gt;     &lt;child&gt;&lt;/child&gt;   &lt;/div&gt; &lt;/template&gt; &lt;script&gt; import Child from &amp;#39;./Child.vue&amp;#39; &lt;/script&gt; //父组件2 &lt;template&gt;   &lt;div&gt;     &lt;child&gt;Hello Juejin&lt;/child&gt;   &lt;/div&gt; &lt;/template&gt; &lt;script&gt; import Child from &amp;#39;./Child.vue&amp;#39; &lt;/script&gt;</pre><div class="contentsignin">Copy after login</div></div>at this time

parent component 1

Display default content

What is a slot? Lets talk about how to use slots in Vue3

Parent component 2

Display provided content

What is a slot? Lets talk about how to use slots in Vue3

NamedSlot

Many times one

slot

cannot meet our needs, we need multiple slots. So there is named slot, which is a slot with a name. To put it simply, the purpose of this Named Slot is to trap a carrot and let them stay where they should. For example, a slot with name <slot name="xx"></slot> is called a named slot. <slot></slot> for which name is not provided will be implicitly named "default". In the parent component, you can use the v-slot:xxx (can be abbreviated as #xxx) directive's <template></template> element to pass the name of the target slot. Go down and match the corresponding slot. For example, <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">//子组件 &lt;template&gt;     &lt;div&gt;         &lt;!-- 大萝卜 --&gt;         &lt;div&gt;             &lt;slot&gt;&lt;/slot&gt;         &lt;/div&gt;         &lt;!-- 小萝卜 --&gt;         &lt;div&gt;             &lt;slot&gt;&lt;/slot&gt;         &lt;/div&gt;         &lt;!-- 中萝卜 --&gt;         &lt;div&gt;             &lt;slot&gt;&lt;/slot&gt;         &lt;/div&gt;     &lt;/div&gt; &lt;/template&gt; //父组件 &lt;template&gt;   &lt;div&gt;     &lt;child&gt;       &lt;!-- #smallTurnip 为v-slot:smallTurnip缩写 --&gt;       &lt;template&gt;         小萝卜       &lt;/template&gt;       &lt;template&gt;         中萝卜       &lt;/template&gt;       &lt;template&gt;         大萝卜       &lt;/template&gt;     &lt;/child&gt;   &lt;/div&gt; &lt;/template&gt; &lt;script&gt; import Child from &amp;#39;./Child.vue&amp;#39; &lt;/script&gt;</pre><div class="contentsignin">Copy after login</div></div>

What is a slot? Lets talk about how to use slots in Vue3So there is no need to care about the order in the parent component. You only need to write the template and name it, and it will automatically go to its corresponding location.

Dynamic slot name

The dynamic slot name is the slot name in the form of a variable. We can modify this variable at any time to show different effects. It is written as

v-slot:[variable name]

or abbreviated as #[variable name]. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">//父组件 &lt;template&gt;   &lt;div&gt;     &lt;child&gt;       &lt;!-- 等同于#smallTurnip --&gt;       &lt;template&gt;         小萝卜       &lt;/template&gt;       &lt;template&gt;         中萝卜       &lt;/template&gt;       &lt;template&gt;         大萝卜       &lt;/template&gt;     &lt;/child&gt;   &lt;/div&gt; &lt;/template&gt; &lt;script&gt; import { ref } from &amp;#39;vue&amp;#39; import Child from &amp;#39;./Child.vue&amp;#39; const slotName = ref(&amp;#39;smallTurnip&amp;#39;) &lt;/script&gt;</pre><div class="contentsignin">Copy after login</div></div>

Scope slot

Scope slot

Said above

Slot content

It is impossible to access the data of subcomponents, but what if we want to access the status of subcomponents in Slot content? In fact,

slot

can be passed to the slot tag by binding properties to the slot content## in the parent component just like passing props to the component. #. First, let’s look at the value transfer method of the default slot

//子组件
<template>
    <div>
        <slot></slot>
    </div>
</template>

//父组件

<template>
  <div>
    <child>
      My name is {{ slotProps.personName }} and I am {{ slotProps.age }} years old this year
    </child>
  </div>
</template>
<script>
import Child from &#39;./Child.vue&#39;
</script>
Copy after login
You can also get the data provided by slot in the form of a structure

<template>
  <div>
    <child>
      My name is {{ personName }} and I am {{ age }} years old this year
    </child>
  </div>
</template>
Copy after login

注意不能绑定name属性,因为你绑定了name它就成了具名插槽了。同样具名插槽中的name属性也不会传递给插槽内容。因为传递的参数只能在插槽内容中使用,所以这类能够接受参数的插槽就被称为了作用域插槽

具名作用域插槽

下面再看下具名作用域插槽它的传参方式。它接收参数的方式是通过template标签的指令v-slot的值获取的,所以可以缩写成这样

//父组件
<template>
  <div>
    <child>
      <template>
        {{ bigTurnipProps.message }}
      </template>
    </child>
  </div>
</template>
<script>
import Child from &#39;./Child.vue&#39;
</script>

//子组件Child.vue

<template>
    <div>
        <!-- 大萝卜 -->
        <div>
            <slot></slot>
        </div>
    </div>
</template>
Copy after login

What is a slot? Lets talk about how to use slots in Vue3

这类插槽便是具名作用域插槽

写在最后

到这里插槽(slot)的全部用法基本就已经介绍完啦。如果感觉对你有用的话赶紧点赞收藏吧!

(学习视频分享:web前端开发编程基础视频

The above is the detailed content of What is a slot? Let's talk about how to use slots in Vue3. For more information, please follow other related articles on the PHP Chinese website!

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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to add functions to buttons for vue How to add functions to buttons for vue Apr 08, 2025 am 08:51 AM

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

How to use watch in vue How to use watch in vue Apr 07, 2025 pm 11:36 PM

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

What does vue multi-page development mean? What does vue multi-page development mean? Apr 07, 2025 pm 11:57 PM

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

How to reference js file with vue.js How to reference js file with vue.js Apr 07, 2025 pm 11:27 PM

There are three ways to refer to JS files in Vue.js: directly specify the path using the &lt;script&gt; tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

How to return to previous page by vue How to return to previous page by vue Apr 07, 2025 pm 11:30 PM

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses &lt;router-link to=&quot;/&quot; component window.history.back(), and the method selection depends on the scene.

How to use vue traversal How to use vue traversal Apr 07, 2025 pm 11:48 PM

There are three common methods for Vue.js to traverse arrays and objects: the v-for directive is used to traverse each element and render templates; the v-bind directive can be used with v-for to dynamically set attribute values ​​for each element; and the .map method can convert array elements into new arrays.

How to jump to the div of vue How to jump to the div of vue Apr 08, 2025 am 09:18 AM

There are two ways to jump div elements in Vue: use Vue Router and add router-link component. Add the @click event listener and call this.$router.push() method to jump.

See all articles