Vue3 manual encapsulation pop-up box component message method

Vue3 manual encapsulation pop-up box component message method

This article shares the specific code of Vue3 manual encapsulation of pop-up box component message for your reference. The specific content is as follows

Package components

<template>
  <Transition name="down">
    <div class="xtx-message" :style="style[type]" v-show='isShow'>
      <!-- The style is bound above -->
      <!-- Different prompt icons will change-->
      <i class="iconfont" :class="[style[type].icon]"></i>
      <span class="text">{{text}}</span>
    </div>
  </Transition>
</template>
<script>
import { onMounted, ref } from 'vue'

export default {
  name: 'XtxMessage',
  props: {
    text: {
      type: String,
      default: ''
    },
    type: {
      type: String,
      // warn warning error error success success default: 'warn'
    }
  },
  setup () {
    // Define an object containing three styles. The object key is the type string const style = {
      warn:
        icon: 'icon-warning',
        color: '#E6A23C',
        backgroundColor: 'rgb(253, 246, 236)',
        borderColor: 'rgb(250, 236, 216)'
      },
      error: {
        icon: 'icon-shanchu',
        color: '#F56C6C',
        backgroundColor: 'rgb(254, 240, 240)',
        borderColor: 'rgb(253, 226, 226)'
      },
      success: {
        icon: 'icon-queren2',
        color: '#67C23A',
        backgroundColor: 'rgb(240, 249, 235)',
        borderColor: 'rgb(225, 243, 216)'
      }
    }
    // Control animation const isShow = ref(false)
    // Trigger onMounted(() => {
      isShow.value = true
    })
    return { style, isShow }
  }
}
</script>
<style scoped lang="less">
.down {
  &-enter {
    &-from {
      transform: translate3d(0, -75px, 0);
      opacity: 0;
    }
    &-active {
      transition: all 0.5s;
    }
    &-to {
      transform: none;
      opacity: 1;
    }
  }
}
.xtx-message {
  width: 300px;
  height: 50px;
  position: fixed;
  z-index: 9999;
  left: 50%;
  margin-left: -150px;
  top: 25px;
  line-height: 50px;
  padding: 0 25px;
  border: 1px solid #e4e4e4;
  background: #f5f5f5;
  color: #999;
  border-radius: 4px;
  i {
    margin-right: 4px;
    vertical-align: middle;
  }
  .text {
    vertical-align: middle;
  }
}
</style>

Mounted on the prototype object of Vue

// The following method needs to render a prompt effect import { createVNode, render } from 'vue'
import XtxMessage from './xtx-message.vue'

// Dynamically create a DOM container const div = document.createElement('div')
div.setAttribute('class', 'xtx-meassage-container')
document.body.appendChild(div)

export default ({ text, type }) => {
  let timer = null
  // createVNode is used to create a virtual node // Parameter 1 supports the component // Parameter 2 represents the options passed to the component const vnode = createVNode(XtxMessage, { text, type })
  // Render the virtual node into the DOM of the page // Parameters of the render function // Parameter 1: indicates the content to be rendered (virtual node)
  // Parameter 2: indicates the target location of rendering (DOM element)
  render(vnode, div)

  // Hope the prompt message will disappear after 1 second clearTimeout(timer)
  timer = setTimeout(() => {
    // Clear the contents of div render(null, div)
  }, 1000)
}

// $message({ text: 'Login failed', type: 'error'})
import Message from './Message'
export default {
  install(app) {
    // If you want to mount global properties, you can call the property this.$message through the component instance
    app.config.globalProperties.$message = Message // prototype function}
}

use

one.

import Message from '@/components/library/Message'
setup () {
    // Trigger login const handleLogin = async () => {
      // Manual form validation const flag = await target.value.validate()
      if (flag) {
        // Verification passed, call the interface to log in // If the login fails, prompt Message({ type: 'error', text: 'Login failed' })
      }
    }
    mounted () {
      this.$message({ type: 'error', text: 'Login failed' })
    }
}

two.

// Get the instance object of the component: similar to the previous this
    const instance = getCurrentInstance()
     // Click to log in const handleLogin = async () => {
      const valid = await target.value.validate()
      if (valid) {
        // Form verification passed, call the interface to log in // Message({ text: 'Login failed', type: 'error' })
        instance.proxy.$message({ text: 'Login failed', type: 'error' })
      }
    }

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of Vue3 encapsulation Message message prompt instance function
  • Vue3 implements Message component example
  • Write a global Message component based on vue
  • Encapsulation and use of el-message in vue

<<:  SQL implements addition, subtraction, multiplication and division operations on two adjacent rows of data

>>:  Excel export always fails in docker environment

Recommend

How to pass the value of the select drop-down box to the id to implement the code

The complete code is as follows : HTML code: Copy ...

CSS3 clear float method example

1. Purpose Through this article, everyone can und...

Quick solution for forgetting MySQL8 password

Preface When we forget the MySQL database passwor...

Create a code example of zabbix monitoring system based on Dockerfile

Use the for loop to import the zabbix image into ...

How to configure Nginx virtual host in CentOS 7.3

Experimental environment A minimally installed Ce...

Mysql auto-increment primary key id is not processed in this way

Mysql auto-increment primary key id does not incr...

JavaScript implements the nine-grid mobile puzzle game

This article shares the specific code for JavaScr...

How to use React slots

Table of contents need Core Idea Two ways to impl...

Why do we need Map when we already have Object in JavaScript?

Table of contents 1. Don’t treat objects as Maps ...

Solution to BT Baota Panel php7.3 and php7.4 not supporting ZipArchive

The solution to the problem that the PHP7.3 versi...

Implementation of Mysql User Rights Management

1. Introduction to MySQL permissions There are 4 ...

Vue implements click feedback instructions for water ripple effect

Table of contents Water wave effect Let's see...