Table of Contents
1. What is Slot
2. Early preparation
3. Use of named slots
1. Sub-component configuration slot" >1. Sub-component configuration slot
{{ title }}
2. User App.vue configuration data" >2. User App.vue configuration data
3. Result display" >3. Result display
4. Named slot summary" > 4. Named slot summary
4. Use of scope slots
#1. Subcomponent configuration slot" >#1. Subcomponent configuration slot
2. The user receives the data and sets the structure" >2. The user receives the data and sets the structure
3. Result display" >3. Result display
4. Scope slot summary" >4. Scope slot summary
Home Web Front-end Vue.js Named slots and scoped slots used in slot analysis in Vue

Named slots and scoped slots used in slot analysis in Vue

Aug 10, 2022 pm 03:32 PM
vue

This article brings you relevant knowledge about vue, which mainly introduces issues related to the use of named slots and scope slots. Slots are provided in sub-components. A placeholder used by the parent component. The slots include default slots, named slots and scope slots. Let’s take a look at them together. I hope it will be helpful to everyone.

Named slots and scoped slots used in slot analysis in Vue

【Related recommendations: javascript video tutorial, vue.js tutorial

1. What is Slot

1. The slot is a placeholder in the child component that is provided to the parent component. It is represented by in the child component. The parent component can be in this Fill the placeholder with any template code, such as HTML, components, etc., and the filled content will replace the tag of the child component. (To put it simply, it is to dig a hole in the subcomponent for others to jump in)

2. After version 2.6.0, slot and slot-scope are uniformly replaced by v-slot.

3. Slots include default slots, named slots and scope slots

2. Early preparation

1、通过vue-cli创建好初始化项目
2、src下创建category.vue,同时在App.vue中引入
Copy after login

3. Use of named slots

1. Sub-component configuration slot

Configure props in the sub-component, receive information from the parent component App and prepare two slot inserts Slot:

//category.vue
<template>
  <div>
    <h3 id="title">{{ title }}</h3>
    //准备两个带有不同name的插槽(可以让使用者在指定的地方显示数据)
    <slot>默认插槽1</slot>
    <slot>默认插槽2</slot>
  </div>
</template>

<script>
export default {
  name: "category",
  data() {
    return {};
  },
  props: ["title", "listData"],
};
</script>

<style>
#bck {
  background-color: skyblue;
  width: 200px;
  height: 300px;
}
h3 {
  text-align: center;
  background-color: #bfa;
}
</style>
Copy after login

2. User App.vue configuration data

By different names defined in the sub-component category, the data can be Display at the specified location:

//App.vue
<template>
  <div>
    <category>
      <img  src="/static/imghw/default1.png" data-src="https://tse2-mm.cn.bing.net/th/id/OIP-C.wwyQPKyRH0ge8-Ppd9DSJgHaEK?w=317&h=180&c=7&r=0&o=5&dpr=1.25&pid=1.7" class="lazy" alt="Named slots and scoped slots used in slot analysis in Vue" >
      <a>更多</a>
    </category>
    <category>
      <ul>
        <li>{{ g }}</li>
      </ul>
      <div>
        <a>单机游戏</a>
        <a>网络游戏</a>
      </div>
    </category>
    <category>
      <video></video>
      <div>
        <a>更多信息1</a>
        <a>更多信息2</a>
      </div>
    </category>
  </div>
</template>

<script>
import category from "./components/category";

export default {
  name: "app",
  data() {
    return {
      foods: ["麻辣烫", "烧烤", "小青龙", "炸酱面"],
      game: ["魔兽世界", "FIFA2016", "NBA2K", "洛克王国"],
    };
  },
  mounted() {},
  methods: {},
  components: {
    category,
  },
};
</script>

<style>
#app,
#game {
  display: flex;
  justify-content: space-around;
}
img {
  width: 100%;
}
video {
  width: 100%;
}
</style>
Copy after login

3. Result display

Named slots and scoped slots used in slot analysis in Vue

4. Named slot summary

1. After defining the slot slot, add slot="name" to the label that needs to be displayed, and it can be displayed at the specified position. Content that needs to be displayed

2. At the same time, it should be noted that the slot data source game of this method is provided in the App parent component, not in the child component itself. In order to reduce redundancy, data can be stored in the component itself that defines the slot through scope slots

4. Use of scope slots

  • Requires the data to be displayed to be placed in the component that defines the slot

  • The parent component App.vue only generates the structure based on the data, and the data is in the component that defines the slot Provided in

#1. Subcomponent configuration slot

The props configured in the subcomponent only need to receive the header. Prepare two scope slots and carry the data to be displayed:

//category.vue
<template>
  <div>
    <h3 id="title">{{ title }}</h3>
    <slot>作用域插槽</slot>
  </div>
</template>

<script>
export default {
  name: "category",
  data() {
    return {
      foods: ["麻辣烫", "烧烤", "小青龙", "炸酱面"],
      games: ["魔兽世界", "FIFA2016", "NBA2K", "洛克王国"],
    };
  },
  props: ["title"],
};
</script>

<style>
#bck {
  background-color: skyblue;
  width: 200px;
  height: 300px;
}
h3 {
  text-align: center;
  background-color: #bfa;
}
</style>
Copy after login

2. The user receives the data and sets the structure

//App.vue
  <category>
      <template>
        <!--ES6解构赋值,{}直接拿到zwt.G的值 -->
        <ul>
          <li>{{ g }}</li>
        </ul></template>      >
    </category>
    <category>
      <template>
        <!--ES6解构赋值,{}直接拿到zwt.F的值 -->
        <ol>
        <li>{{f}}</li>
        </ol>
      </template>
    </category>
Copy after login

3. Result display

Named slots and scoped slots used in slot analysis in Vue

4. Scope slot summary

1 , can solve the problem that there is no data to be displayed in the user component, and can be used when you want to call the data of other components.

2. The component that defines the slot passes its own data to the user, and the user configures the structure after receiving the data.

3. The user only determines the generated structure style, and the data is passed from the user (the component that defines the slot).

4. It can be understood that slot means that the parent component inserts a specific structure into the specified position of the child component.

【Related recommendations: javascript video tutorial, vue.js tutorial

The above is the detailed content of Named slots and scoped slots used in slot analysis in Vue. 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 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 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 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 query the version of vue How to query the version of vue Apr 07, 2025 pm 11:24 PM

You can query the Vue version by using Vue Devtools to view the Vue tab in the browser's console. Use npm to run the "npm list -g vue" command. Find the Vue item in the "dependencies" object of the package.json file. For Vue CLI projects, run the "vue --version" command. Check the version information in the &lt;script&gt; tag in the HTML file that refers to the Vue file.

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.

See all articles