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

SpringBoot integrates Activiti7 implementation code

After the official release of Activiti7, it has f...

Summary of the minesweeping project implemented in JS

This article shares the summary of the JS mineswe...

How to change the domestic image source for Docker

Configure the accelerator for the Docker daemon S...

Detailed explanation of WeChat Mini Program official face verification

The mini program collected user personal informat...

Implementation of Docker cross-host network (overlay)

1. Docker cross-host communication Docker cross-h...

How to use js to communicate between two html windows

Scenario: When page A opens page B, after operati...

How to click on the a tag to pop up the input file upload dialog box

html Copy code The code is as follows: <SPAN cl...

How to deploy FastDFS in Docker

Install fastdfs on Docker Mount directory -v /e/f...

CSS performance optimization - detailed explanation of will-change usage

will-change tells the browser what changes will h...

MySQL 5.7.18 Archive compressed version installation tutorial

This article shares the specific method of instal...

CSS3 realizes the glowing border effect

Operation effect: html <!-- This element is no...

Nine advanced methods for deduplicating JS arrays (proven and effective)

Preface The general methods are not listed here, ...

How to build php-nginx-alpine image from scratch in Docker

Although I have run some projects in Docker envir...