How to use slots in Vue to distribute parent components
Jun 01, 2018 pm 01:48 PMThis 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-tipVue.component('dialog-tip', { template: '#dialog-tip', props:['dialogShow','message'], data:function(){ return { content:'' } }, methods:{ } });
<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>
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>
< ;slot name="msg">Please enter any integer between 1-8000</slot>
<p slot="msg">Please enter the correct mobile phone number</p>
<dialog-tip message="hello" :dialog-show="dialogShow.tip3"> <p slot="msg">请输入正确手机号</p> <button class="btn" @click="closeDialogTip('tip3')">确定</button> </dialog-tip> <dialog-tip message="hello" :dialog-show="dialogShow.tip4"> <p slot="msg">抱歉,没有此用户,请核实后输入</p> <button class="btn" @click="closeDialogTip('tip4')">重新输入</button> <button class="btn" @click="reg">去注册</button> </dialog-tip>
does not define a named slot.
https://cn.vuejs.org/v2/guide/components-slots.html
FinalRendering
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use v-slot default slot in Vue

The ultimate evolution of Python applications: PyInstaller emerges from the cocoon and becomes a butterfly

Transform Python code into an independent application: the alchemy of PyInstaller

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

Detailed explanation of slot function in Vue3: using slots to implement more flexible components

Using slots in Vue to achieve flexible layout of components
