Vue implements custom "modal pop-up window" component example code

Vue implements custom "modal pop-up window" component example code

Preface

The dialog box is a very common component and is used in many places. Generally, we can use the built-in alert to pop up the dialog box, but what if it is a designed diagram? So we need to write a dialog box ourselves. Let's take a look at the detailed implementation process.

Rendering

In the above screenshot, the red border indicates that the "text, icon or image" is the editable part.

Example Code

1. Create a pop-up component quitDialog.vue component

<template>
  <transition-group name='fade'>
    <!-- Exit pop-up window-->
    <div class="quit_dialog"
         key="1"
         @click="isQuit = false"
         v-if="isQuit"
         @touchmove.prevent>
    </div>
    <div class="quit_box"
         v-show="isQuit"
         key="2">
      <img :src="imgUrl"
           :alt="imgLoadTip">
           <div class="quit_title">{{title}}</div>
      <p>{{content}}</p>
      <button class="quit_btn" @click="leftClick">{{btnText}}</button>
      <button class="quit_close" @click="rightClick">{{rightText}}</button>
    </div>
  </transition-group>
</template>
<script>
export default {
  name: 'Popup',
  data () {
    return {
      isQuit: false,
      imgUrl: '',
      title: '',
      content: '',
      btnText: '',
      rightText: ''
    }
  },
  methods: {
    leftClick () {
      this.leftBtn()
      this.isQuit = false
    },
    rightClick () {
      this.rightBtn()
      this.isQuit = false
    }
  }
}
</script>
<style lang="scss" scoped>
// Exit pop-up window.fade-enter,
.fade-leave-active {
  opacity: 0;
}
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.35s;
}
// Global pop-up window.quit_dialog {
  background: rgba(0,0,0,.5);
  position: fixed;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  z-index: 10000;
}
 
.quit_box {
  width: 700px;
  background: #fff;
  position: fixed;
  top: 50%;
  left: 50%;
  margin-left: -350px;
  margin-top: -190px;
  z-index: 10001;
  border-radius: 10px;
  text-align: center;
  padding: 50px;
  img{
    width: 80px;
  }
 .quit_title{
      color: #666;
      font-size: 28px;
      margin: 45px 0px;
  }
  button {
    border-radius: 32px;
    padding:20px 0px;
    font-size: 26px;
    border-radius: 8px;
    width: 214px;
  }
  .quit_btn{
    color: #03BA82;
    background: #fff;
    border: 1px solid #03BA82;
    margin-right: 32px;
  }
  .quit_close {
    background: linear-gradient(0deg, #03BA82, #01D695);
    box-shadow: 0px 3px 4px 0px rgba(1, 84, 58, 0.27);
    border: 1px solid #03BA82;
    color: #fff;
  }
}
</style>

2. Create grabDialog.js

import Vue from 'vue'
import Grasp from '../components/QuitDialog/QuitDialog'
 
const PopupBox = Vue.extend(Grasp)
 
Grasp.install = function (data) {
  let instance = new PopupBox({
    data
  }).$mount()
 
  document.body.appendChild(instance.$el)
 
  Vue.nextTick(() => {
    instance.isQuit = true
    // isQuit corresponds to isQuit in the pop-up component and is used to control visibility})
}
 
export default Grasp

3. Import in global main.js

import Vue from 'vue'
import Popup from './api/quitDialog'
Vue.prototype.$popup = Popup.install

4. To call in the page, just call in the function

methods: {
    graspBtn () {
      this.$grasp({
        imgUrl: require('../../assets/home/quits.png'), // Top image.
        imgLoadTip: 'Image loading...',
        content: 'Warm Tips',
        title: 'Note: This learning task is not completed, do you confirm to exit',
        btnText: 'Cruel Exit',
        rightText: 'Continue to study',
        // Left click event leftBtn: () => {
          this.$store.dispatch('user/logout').then(() => {
            this.$signalr.LogoutPad()
            this.$signalr.SendMsg(2, 0, 'Exit system')
            this.$router.push('/login')
          })
        },
        // Right click event rightBtn: () => {}
      })
    }
}

Summarize

This is the end of this article about how to implement custom "modal pop-up window" components in Vue. For more relevant content about custom "modal pop-up window" components in Vue, 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 custom global pop-up component operation

<<:  CSS3 timeline animation

>>:  Seven solutions for classic distributed transactions between MySQL and Golan

Recommend

Solution to the Docker container cannot be stopped and deleted

Find the running container id docker ps Find the ...

Build a Scala environment under Linux and write a simple Scala program

It is very simple to install Scala environment in...

The magic of tbody tag speeds up the display of table content

You must have saved other people’s web pages and l...

How to solve the Mysql transaction operation failure

How to solve the Mysql transaction operation fail...

An Uncommon Error and Solution for SQL Server Full Backup

1. Error details Once when manually performing a ...

Detailed explanation of the JavaScript timer principle

Table of contents 1. setTimeout() timer 2. Stop t...

Windows Server 2016 Standard Key activation key serial number

I would like to share the Windows Server 2016 act...

Future-oriented all-round web design: progressive enhancement

<br />Original: Understanding Progressive En...

Detailed analysis of the blocking problem of js and css

Table of contents DOMContentLoaded and load What ...

CenOS6.7 mysql 8.0.22 installation and configuration method graphic tutorial

CenOS6.7 installs MySQL8.0.22 (recommended collec...

An article to help you understand Js inheritance and prototype chain

Table of contents Inheritance and prototype chain...

MySQL 8.0.23 installation and configuration method graphic tutorial under win10

This article shares the installation and configur...