SoFunction
Updated on 2025-03-09

vue3 method to manually encapsulate pop-up box component message

This article shares the specific code of the message of the vue3 manual encapsulation pop-up box component 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 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 error error success      default: 'warn'
    }
  },
  setup () {
    // Define an object that contains three styles. The object key is a 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)
    // Triggered after the component template is rendered successfully    onMounted(() => {
       = 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>

Mount to the prototype object of vue

// The following method requires rendering a prompt effectimport { createVNode, render } from 'vue'
import XtxMessage from './'

// Dynamically create a DOM containerconst div = ('div')
('class', 'xtx-meassage-container')
(div)

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

  // I hope the prompt message will disappear after 1 second  clearTimeout(timer)
  timer = setTimeout(() => {
    // Clear the contents in the 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 properties this.$message through the component instance    .$message = Message // Prototype function  }
}

use

one .

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

two.

// Get the component's instance object: similar to this previous    const instance = getCurrentInstance()
     // Click to log in    const handleLogin = async () => {
      const valid = await ()
      if (valid) {
        // The form verification is passed, and the interface is called to realize login        // Message({ text: 'Login failed', type: 'error' })        .$message({ text: 'Login failed', type: 'error' })
      }
    }

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.