Detailed explanation of Vue3 encapsulation Message message prompt instance function

Detailed explanation of Vue3 encapsulation Message message prompt instance function

Vue3 encapsulation message prompt instance function

  • Vue2.0 uses Vue.prototype.$message = function () {}
  • vue3.0 uses app.config.globalProperties to mount the prototype method app.config.globalProperties.$message = Message
  • It also supports directly importing functions using import Message from './Message.js

Style layout encapsulation message.vue

<template>
  <Transition name="down">
    <div class="my-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: 'myMessage',
  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;
    }
  }
}
.my-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>

Function implementation message.js

//icon//textimport { createVNode,render } from 'vue'
import myMessage from './message.vue'
// Dynamically create a DOM container const div = document.createElement('div')
div.setAttribute('class','my-message-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(myMessage,{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 it disappears after 1s clearTimeout(timer)
   timer = setTimeout(()=>{
     // Clear the contents of div render(null,div)
   },1000)
}
// $message({ text: 'Login failed', type: 'error'})

Registering custom directives

import Message from './Message.js'
export default {
  install(app){
  // If you want to mount global properties, you can call the property this.$message through the component instance
     // Extend an instance method app.config.globalProperties.$message = Message // prototype function }
}

use:

Method 1

import Message from './message.js'
setup(){
  Message({ text: 'Login failed', type: 'error' })
}

Method 2

// Access the component instance object in the setup function import { getCurrentInstance } from 'vue'
   setup(){
     const instance = getCurrentInstance()
     instance.proxy.$message({ text: 'Login failed', type: 'error' })
   } 

Method 3 this.$message

this.$message({ text: 'Login failed', type: 'error' })

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Vue3 manual encapsulation pop-up box component message method
  • Vue3 implements Message component example
  • Write a global Message component based on vue
  • Encapsulation and use of el-message in vue

<<:  Reasons for the sudden drop in MySQL performance

>>:  Implementation of Docker deployment of MySQL cluster

Recommend

JavaScript to achieve uniform animation effect

This article example shares the specific code for...

New settings for text and fonts in CSS3

Text Shadow text-shadow: horizontal offset vertic...

Three solutions for sub-functions accessing external variables in JavaScript

Preface When we write web pages, we will definite...

Docker installation and configuration command code examples

Docker installation Install dependency packages s...

Native JS to implement login box email prompt

This article shares a native JS implementation of...

About the location of the H1 tag in XHTML

There has been a lot of discussion about H1 recent...

How to implement html input drop-down menu

Copy code The code is as follows: <html> &l...

How to install setup.py program in linux

First execute the command: [root@mini61 setuptool...

VMware configuration VMnet8 network method steps

Table of contents 1. Introduction 2. Configuratio...

Example of making a butterfly flapping its wings with pure CSS3

Pure CSS3 makes a butterfly flapping its wings, s...

Analysis and solution of data loss during Vue component value transfer

Preface In the previous article Two data types in...

WeChat applet realizes the nine-square grid effect

This article shares the specific code for the WeC...