Detailed analysis of the usage and application scenarios of slots in Vue

Detailed analysis of the usage and application scenarios of slots in Vue

What are slots?

We know that in Vue, nothing can be wrapped in the middle of the Child component tag.

But often when we use components, we always want to customize some tags outside the components. Vue has added a slot mechanism called scope slot. The required version is 2.1.0+;

Slots are actually placeholders. It takes up a place in your HTML template in the component and lets you pass something in. Slots are divided into anonymous slots, named slots, and scoped slots.

In 2.6.0, we introduced a new unified syntax for named and scoped slots (the v-slot directive). It replaces slot and slot-scope

Anonymous Slots

Anonymous slot, we can also call it a single slot or default slot. Compared with named slots, it does not need to set the name attribute, and its hidden name attribute is default.

father.vue

child.vue

For anonymous slots, the name attribute corresponds to default, and you can also leave it blank to use the default value.

There is another problem to note when using it. If there are more than 2 anonymous slots, the contents of the child tag will be replaced in each slot;

Named slots (vue2.6.0+ deprecated slot='name')

As the name implies, slots are named. Definition: Or use a simple abbreviation to define #header. Use: Wrap it with a template tag.

father.vue

child.vue

Here is a discussion of the use of multiple named slots. The position of the slot is not determined by the position of the slot being used, but is replaced by the position when it is defined.

father.vue

child.vue

Scoped Slots

It is the slot used to transfer data.

When you want to use data in a slot, you need to pay attention to the problem of scope. The Vue official documentation says that all content in the parent template is compiled in the parent scope; all content in the child template is compiled in the child scope;

To make the data in the child component available in the parent's slot content, we can bind the data as a property of the element: v-bind:text="text"

Notice:

The difference between anonymous scope slots and named scope slots is v-slot:defult="accepted name" (defult (anonymous can be omitted, the opposite of named is to write the corresponding name))

v-solt can deconstruct and receive. The fields received by deconstruction must be the same as the fields passed. For example: one corresponds to v-slot="{one}"

Rendering

Summarize

This concludes this article about the usage and application scenarios of slots in Vue. For more information about the usage of Vue slots, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue elementui implements the example code of the search bar public component encapsulation
  • Detailed explanation of how to encapsulate your own component in vue element-ui
  • Complete step record of Vue encapsulation of general table components
  • How to use vue slot to encapsulate common components in vue

<<:  Basic knowledge points of mysql worm replication

>>:  Briefly describe the installation of influxDB distributed time series database and related operations in Docker

Recommend

Basic commands for MySQL database operations

1. Create a database: create data data _name; Two...

Detailed explanation of daily_routine example code in Linux

First look at the example code: #/bin/bash cal da...

Vue.js framework implements shopping cart function

This article shares the specific code of Vue.js f...

Installation tutorial of MySQL 5.7.17 zip package version under win10

The installation tutorial of mysql5.7.17 is share...

Detailed explanation of VUE Token's invalidation process

Table of contents Target Thought Analysis Code la...

JavaScript to achieve simple drag effect

This article shares the specific code of JavaScri...

CSS implements the bottom tapbar function

Now many mobile phones have the function of switc...

Detailed steps for using jib for docker deployment in Spring Cloud

Introduction to Jib Jib is a library developed by...

Detailed Linux installation tutorial

(Win7 system) VMware virtual machine installation...

How does Zabbix monitor and obtain network device data through ssh?

Scenario simulation: The operation and maintenanc...

Nginx builds rtmp live server implementation code

1. Create a new rtmp directory in the nginx sourc...

Master-slave synchronization configuration of Mysql database

Table of contents Mysql master-slave synchronizat...