search
HomeWeb Front-endJS TutorialDetailed explanation of the steps to implement the modal dialog component in Vue

This time I will bring you a detailed explanation of the steps to implement the modal dialog component in vue. What are the precautions for the implementation of the modal dialog component in vue. The following is a practical case, let's take a look.

Written in front

Dialog box is a very commonly used component and is used in many places. Generally, we can use the built-in alert to pop up the dialog box, but What should we do if it is a designed diagram, so we need to write a dialog box ourselves, and if it is used in many places, then it is necessary for us to write it in a common component form and reference it between places where it is needed.

Now let’s implement a dialog component. According to the previous habit, let’s take a look at the implementation renderings


1. First, define a component through template

<template>
    <p>
      </p>
<p></p>
      <p>
        </p>
<p>
          </p>
<p>提示内容</p>
          <p>
            <a>确定</a>
            <a>取消</a>
            <a>去登录</a>
          </p>
        
      
    
  </template>

and add the corresponding dialog style

/*对话框style*/
    .dialog{
    }
    .dialog_mask{
      position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.5);
    }
    .dialog_container{
        background: #fff;
  width: 300px;
  height: 120px;
  position: relative;
  border-radius: 10px;
  margin: 0 auto;
    }
    .dialog_content{
      text-align: center;
  padding-top: 30px;
    }
    .dialog_btn{
      margin-top: 20px;
    }
    .dialog_btn a{
      background: yellow;
        padding: 2px 30px;
  border-radius: 5px;
  color: #fff;
  text-decoration: none;
    width: 50px;
  display: inline-block;
    }
    .dialog_btn a:nth-child(2){
        margin-left: 20px;
    }

2. Use Vue.component to register a global Vue component , we call this component v-dialog, and then specify this component through template

Vue.component('v-dialog', {
      template: '#dialog',
      data:function(){
        return {
        }
      },
      methods:{
      },
      created:function(){
      }
    })

3. Finally, reference this component through the v-dialog tag where we need it

&lt;v-dialog&gt;&lt;/v-dialog&gt;

The steps to create a vue component are roughly like this, but how should the parent component and child components communicate? Woolen cloth?

Here we mainly use props to transfer data to sub-components

Modify the above code as follows and add the props attribute

Vue.component('v-dialog', {
      template: '#dialog',
          props:['dialogShow','msg'],
      data:function(){
        return {
        }
      },
      methods:{
      },
      created:function(){
      }
    })

You can see that we define prop through a string array In addition, we can also define props in the form of objects to specify validation rules for component props. If the type is wrong, there will be a warning in vue, where the value of type can be these :String Number Boolean Function Object Array Symbol

props: {
    name: String,
    showDialog: {
      type: Boolean,
      default: false
    }
   }

In the component template, use v-if="showDialog" to determine whether to display or hide the dialog box, and use v-text=" msg"Bind the dialog box prompt content,

v-if="type==1" is used to determine the dialog box type and display the corresponding button. The code is as follows:

<template>
    <p>
      </p>
<p></p>
      <p>
        </p>
<p>
          </p>
<p>提示内容</p>
          <p>
            <a>确定</a>
            <a>取消</a>
            <a>去登录</a>
          </p>
        
      
    
  </template>

Add:show-dialog="showDialog" :msg="msg" :type="type" where the component is referenced, and pass their values ​​to the dialog component

&lt;v-dialog&gt;&lt;/v-dialog&gt;

It should be noted that showDialog needs to be written in the form of show-dialog in the component, otherwise the data will not be obtained.

We define these attributes in the data

data: {
        msg:'',
        showDialog:false,
        type:1,// 提示类型 1单按钮提示框 2双按钮提示框
      },

Then, we add the button When clicking submit, the pop-up dialog box event is triggered

submit:function(){
          //弹出对话框组件
          if(!this.isLogin){//未登录
            this.msg = "请先去登录再领取金额";
            this.showDialog = !this.showDialog;
            this.type = 2;
            return;
          }
          if(this.amount){
            if(this.amount1000){
              this.msg = "输入金额不能低于1元大于1000";
              this.showDialog = !this.showDialog;
              this.type = 1;
            }else{
              this.msg = "领取成功,请在账户中心查看";
              this.showDialog = !this.showDialog;
              this.type = 1;
            }
          }else{
            this.msg = "领取金额不能为空";
            this.showDialog = !this.showDialog;
              this.type = 1;
          }
        }

In this way, we can pop up the dialog box component and set different prompt messages through msg

So, how do we close this dialog box? , here it involves the child component needing to transmit information to the parent component

Mainly trigger parent class events through $emit, such as: this.$emit('close-dialog'); Then use v-on in the parent class to monitor the events triggered by the subclass, v-on:close-dialog="closeDialog", which can also be abbreviated as @close-dialog="closeDialog"

The code is as follows:

Add @close-dialog="closeDialog" in the v-dialog tag to listen for events triggered by sub-components

&lt;v-dialog&gt;&lt;/v-dialog&gt;

Then define the closeDialog function to modify showDialog status                                                                                                                                                                                                                                                  It also needs to be noted that the listening function closeDialog needs to be written in the form of close-dialog

ok, above we have implemented a dialog component

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

Recommended reading:

Detailed explanation of the steps to implement full-screen scrolling plug-in in ES6


Summary of how to use nodejs log module winston

The above is the detailed content of Detailed explanation of the steps to implement the modal dialog component in Vue. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
如何修复暗黑破坏神4蓝屏错误在Windows 11 / 10如何修复暗黑破坏神4蓝屏错误在Windows 11 / 10Jun 27, 2023 pm 01:16 PM

玩暗黑破坏神4时遇到蓝屏问题?好吧,您不是唯一一个在Reddit或其他论坛上抱怨此问题的人。只有当某些关键的系统组件无法承受暗黑破坏神4的要求时,蓝屏才会出现。因此,我们建议您按照这些解决方案快速解决问题并开始享受游戏。修复1–确保您的系统具有最少的支持暗黑破坏神4是一款对图形要求非常高的游戏,即使是最低的系统要求也令人困惑。这些是运行暗黑破坏神4的最低、推荐和超4k要求。最低要求–操作系统:64位Windows®10版本1909或更高版本处理器:英特尔®酷睿i5-2500K或AMDFX-835

如何在 Windows PC 上修复 Steam 登录错误 E84如何在 Windows PC 上修复 Steam 登录错误 E84Jun 28, 2023 am 08:20 AM

Steam登录错误E84是Steam用户在多次登录尝试中遇到的常见登录。如果您无法登录Steam,则无法执行任何有用的操作。如果您不先处理此E84登录错误,您将面临大量问题。初步解决方法–1.如果您是第一次在Steam中遇到此E84错误,重新启动系统可能会修复它。关闭Steam应用程序。将其从系统托盘中退出。然后,重新启动系统并重试整个过程。2.检查互联网连接是否有故障。如果您的互联网连接速度较慢,Steam登录可能会引发E84。修复1–将noreactlogin添加到Steam可执行文件您必须

如何修复写字板在 Windows 11/10 中无法打开的问题如何修复写字板在 Windows 11/10 中无法打开的问题Jun 28, 2023 am 08:51 AM

写字板是继记事本之后最快的工具,可以记下您丰富多彩的想法。但是,如果无法在计算机上打开写字板怎么办?写字板通常运行良好,并且打开速度非常快。但是,如果您的系统中缺少任何关键的写字板组件,写字板将无法打开。按照以下几组解决方案修复计算机上的问题。注意-由于写字板预安装在Windows上,因此您无法像执行任何其他本机应用商店应用程序那样直接重置或修复它。因此,只有一组最少的解决方案可用于解决问题。修复1–直接运行写字板您可以直接从安装目录运行写字板,并检查这是否有助于解决问题。步骤1–您需要打开文件

如何为您的 Windows lComputer 设置首选频段 [2023]如何为您的 Windows lComputer 设置首选频段 [2023]Jun 26, 2023 am 08:26 AM

几乎所有最新品牌的笔记本电脑都配备了双品牌WiFi。您可以将WiFi设置为5GHz或2.4GHz带宽。但是,事情并没有那么简单。笔记本电脑上的此功能很好地隐藏在设备管理器中,您无法从“设置”页面执行此操作。按照我们的指南为您的笔记本电脑、PC设置首选频段。注意–要切换到5GHz带宽WiFi,您需要WiFi路由器和设备都支持双频WiFi。如果它们中的任何一个都没有支持,则无法更改WiFi带宽。如何在设备上设置首选的WiFi频段设置首选频段以充分利用您的WiFi非常容易。方式1–设置首选频段步骤1–

如何检查iPhone型号国家如何检查iPhone型号国家Jul 09, 2023 pm 11:33 PM

您知道苹果将其产品的某些部分外包给不同的国家吗?是的。它们专门用于在这些国家/地区销售,因此在该国制造。您可能从其他人那里购买了二手iPhone/iPad,并且可能想知道是否有可能知道您的iPhone来自哪个国家。是的,有一种方法可以找出答案,我们现在将在本文中对此进行更多讨论。在这篇文章中,您将找到解释如何使用简单步骤了解iPhone原产国的方法。如何知道iPhone的原产国步骤1:首先,您应该点击主屏幕中的设置图标。第2步:这是打开“设置”应用程序,打开后,单击它转到“常规”选项,如下所示。

从 Windows 10/11 中删除用户帐户的 5大方法 [2023]从 Windows 10/11 中删除用户帐户的 5大方法 [2023]Jun 27, 2023 am 08:34 AM

您的WindowsPC上有多个过时的帐户?或者,由于某些错误,您是否在从系统中删除这些帐户时陷入困境?无论出于何种原因,您都应该尽快从计算机中删除那些未使用的用户帐户。这样,您将节省大量空间并修复系统中可能的漏洞点。在本文中,我们通过详细步骤详细阐述了多种用户帐户删除方法。方法1–使用设置这是从系统中删除任何帐户的标准方法。步骤1–按Win+I键应打开“设置”窗口。步骤2–转到“帐户”。第3步–找到“其他用户”将其打开。第4步–您将在屏幕右侧找到所有帐户。步骤5–只需在那里扩展帐户即可。在帐户和

如何在iPhone上提取RAR文件如何在iPhone上提取RAR文件Jul 12, 2023 pm 07:53 PM

很多时候,非常大的文件很难在设备之间共享,尤其是智能手机等。因此,这些文件首先被存档/压缩成RAR文件,然后发送到另一个设备进行共享。但问题是RAR文件不容易在iPhone上提取。要提取zip文件,只需轻点一下即可。没有多少人知道在iPhone上提取RAR文件的过程,对于初学者来说,这些步骤可能会令人困惑。可以使用iPhone上称为快捷方式的默认应用程序来完成此操作。我们在这里逐步解释了如何使用快捷方式应用程序在iPhone上提取任何RAR文件。如何在iPhone上提取RAR文件步骤1:首先,您

u盘怎么重装win11系统的步骤教程u盘怎么重装win11系统的步骤教程Jul 08, 2023 pm 09:33 PM

微软近日透露了将推出win11系统,很多用户都在期待新系统呢。网上已经有泄露关于win11的镜像安装系统。大家不知道如何安装的话,可以使用U盘来进行安装。小编现在就给大家带来了win11的U盘安装教程。1、首先准备一个8G以上大小的u盘,将它制作成系统盘。2、接着下载win11系统镜像文件,将它放入u盘中,大家可以直接点击右侧的链接进行下载。3、下载完成后装载该iso文件。4、装载完成之后会进入新的文件夹,在其中找到并运行win11的安装程序。5、在列表中选择“win11”然后点击“下一步”。6

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!