Home Web Front-end JS Tutorial How to use slots in Vue to distribute parent components

How to use slots in Vue to distribute parent components

Jun 01, 2018 pm 01:48 PM
slot distribution slot

This time I will show you how to use the slot slot in Vue to distribute the parent component, and what are the things to note . Here is a practical case, let’s take a look. .

Written before

I have previously written an article about Vue’s implementation of the dialog box component http://www.jb51.net /article/139218.htm

talked about how to implement a vue dialog component, which involves the communication between the parent component and the child component. Needless to say, this can be understood by reading my previous article. The article also said at the end Now, we can use slot slots to write components. Slots are used to distribute content to sub-components, thereby achieving a high degree of reuse of components and making the components written more flexible.

Still using the example of a dialog box, use slot to implement the dialog component

Register a global component named dialog-tip

 Vue.component('dialog-tip', {
   template: '#dialog-tip',
   props:['dialogShow','message'],
   data:function(){
    return {
     content:''
    }
   },
   methods:{
   }
  });
Copy after login
Use the templet tag to define this component

<template id="dialog-tip">
  <p class="dialog_tip" v-if="dialogShow">
   <p class="dialog_tip--mask"></p>
   <p class="dialog_tip--content">
    <p class="dialog_tip--contenttxt">
     <slot name="msg">请输入1-8000之间任意整数</slot>
    </p>
    <p class="dialog_tip--contentbtns">
     <slot>
      <button class="btn">确定</button>
      <button class="btn">重新输入</button>
      <button class="btn">去注册</button>
     </slot>
    </p>
   </p>
  </p>
 </template>
<template id="dialog-tip">
  <p class="dialog_tip" v-if="dialogShow">
   <p class="dialog_tip--mask"></p>
   <p class="dialog_tip--content">
    <p class="dialog_tip--contenttxt">
     <slot name="msg">请输入1-8000之间任意整数</slot>
    </p>
    <p class="dialog_tip--contentbtns">
     <slot>
      <button class="btn">确定</button>
      <button class="btn">重新输入</button>
      <button class="btn">去注册</button>
     </slot>
    </p>
   </p>
  </p>
 </template>
Copy after login
The component content includes two parts, one is the prompt content, and the other is the button button. We will use slot to include the content to be modified and replaced,

so that the parent component can distribute the content to the child component.

<p class="dialog_tip--contenttxt">
     <slot name="msg">请输入1-8000之间任意整数</slot>
    </p>
    <p class="dialog_tip--contentbtns">
     <slot>
      <button class="btn">确定</button>
      <button class="btn">重新输入</button>
      <button class="btn">去注册</button>
     </slot>
    </p>
Copy after login
In addition to the default slots, you can also define named slots. If there are several parts of the component that need to be replaced, we can define a name for it, for example:

< ;slot name="msg">Please enter any integer between 1-8000

In this way, when using the component, specify the name of the slot, and this part of the content will be Replace it without replacing other slot contents

Please enter the correct mobile phone number

Use the defined dialog component

<dialog-tip message="hello" :dialog-show="dialogShow.tip3">
   <p slot="msg">请输入正确手机号</p>
   <button class="btn" @click="closeDialogTip(&#39;tip3&#39;)">确定</button>
  </dialog-tip>
  <dialog-tip message="hello" :dialog-show="dialogShow.tip4">
   <p slot="msg">抱歉,没有此用户,请核实后输入</p>
   <button class="btn" @click="closeDialogTip(&#39;tip4&#39;)">重新输入</button>
   <button class="btn" @click="reg">去注册</button>
  </dialog-tip>
Copy after login
If you do not specify the name of the slot, the content in the default dialog-tip tag will replace the content contained in the sub-component using slot, for example, the above

Use slot Specify its name to replace the corresponding slot part in the subcomponent, and content that does not use slot to specify the name will by default replace the part of the subcomponent that

does not define a named slot.

It should be noted that if the content that needs to be distributed is not defined in the dialog-tip tag, the default slot content will be displayed in the sub-component

For more slot usage, please move Step

https://cn.vuejs.org/v2/guide/components-slots.html

Final

Rendering

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Vue.js internal listener use case analysis

How to use Vue SSR component loading

The above is the detailed content of How to use slots in Vue to distribute parent components. 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 v-slot default slot in Vue How to use v-slot default slot in Vue Jun 11, 2023 am 09:27 AM

Vue is a popular front-end framework that provides many instructions to help us develop better. Among them, v-slot is a very important instruction, which allows us to combine components more flexibly and improve the readability and reusability of the code. The default slot is a slot type in v-slot. Using the default slot, you can pass the HTML structure in the parent component to the child component, so that the child component can render it as its own child element. This article will introduce you in detail how to use v- in Vue

The ultimate evolution of Python applications: PyInstaller emerges from the cocoon and becomes a butterfly The ultimate evolution of Python applications: PyInstaller emerges from the cocoon and becomes a butterfly Feb 19, 2024 pm 03:27 PM

PyInstaller is a revolutionary tool that empowers Python applications beyond their original scripting form. By compiling Python code into standalone executable files, PyInstaller unlocks a new realm of code distribution, deployment, and maintenance. From a single script to a powerful application In the past, Python scripts only existed in a specific Python environment. Distributing such a script requires users to install Python and the necessary libraries, which is a time-consuming and cumbersome process. PyInstaller introduces the concept of packaging, combining Python code with all required dependencies into a single executable file. The Art of Code Packaging PyInstaller’s Work

Transform Python code into an independent application: the alchemy of PyInstaller Transform Python code into an independent application: the alchemy of PyInstaller Feb 19, 2024 pm 01:27 PM

PyInstaller is an open source library that allows developers to compile Python code into platform-independent self-contained executables (.exe or .app). It does this by packaging Python code, dependencies, and supporting files together to create standalone applications that can run without installing a Python interpreter. The advantage of PyInstaller is that it removes dependency on the Python environment, allowing applications to be easily distributed and deployed to end users. It also provides a builder mode that allows users to customize the application's settings, icons, resource files, and environment variables. Install PyInstal using PyInstaller to package Python code

What problem does vue slot solve? What problem does vue slot solve? Jan 13, 2023 pm 06:26 PM

The problem solved by vue slot: Content is not allowed to be written in the middle of the introduced subcomponent tags. Slot is a capability provided by Vue for component packagers; it allows developers to define uncertain parts that are expected to be specified by the user as slots when packaging components; slots can be considered as during component packaging. A placeholder for content reserved for the user.

Detailed explanation of slot function in Vue3: using slots to implement more flexible components Detailed explanation of slot function in Vue3: using slots to implement more flexible components Jun 18, 2023 am 09:29 AM

Vue is a popular JavaScript front-end framework. Its third version (Vue3) introduces many new features, one of which is the slot function. This article will explain in detail what the slot function is and how to use it to implement more flexible components. What is the slot function? In Vue, a component is an abstracted part of a page element. A component can be included in other components. Normally, the content of a component is fixed, but sometimes we want the content of the component to be variable. This is slo

Using slots to optimize component expansion performance in Vue Using slots to optimize component expansion performance in Vue Jul 17, 2023 pm 03:16 PM

Using slots in Vue to optimize the scalability of components In Vue development, components are important modules for building application interfaces. A powerful and effective component can improve development efficiency and code reusability. However, as the size of the application increases, component scalability often becomes a challenge. To solve this problem, Vue provides a powerful feature - slot. Slots are a mechanism in Vue for delivering content to components. By using slots, we can take the flexibility and reusability of our code even further

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

I believe that everyone who has used Vue has used slots in Vue more or less, but do you understand all its uses? This article will bring you all the usage of slots in Vue3 to help you find and fill gaps.

How to monitor Vue slot changes? Try this trick! How to monitor Vue slot changes? Try this trick! Sep 28, 2022 pm 07:52 PM

How to monitor Vue slot changes? The following article will introduce you to the method of monitoring Vue slot changes. I hope it will be helpful to you!

See all articles