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

docker cp copy files and enter the container

Enter the running container # Enter the container...

Detailed introduction to logs in Linux system

Table of contents 1. Log related services 2. Comm...

Detailed explanation of the usage of setUp and reactive functions in vue3

1. When to execute setUp We all know that vue3 ca...

Detailed tutorial on setting password for MySQL free installation version

Method 1: Use the SET PASSWORD command MySQL -u r...

The difference between mysql outer join and inner join query

The syntax for an outer join is as follows: SELEC...

JavaScript array deduplication solution

Table of contents Method 1: set: It is not a data...

Write a formal blog using XHTML CSS

The full name of Blog should be Web log, which mea...

Bootstrap 3.0 study notes CSS related supplement

The main contents of this article are as follows:...

Nginx/Httpd reverse proxy tomcat configuration tutorial

In the previous blog, we learned about the usage ...

Simply learn various SQL joins

The SQL JOIN clause is used to join rows from two...

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

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

How to design the homepage of Tudou.com

<br />I have been working in front-end for s...

Detailed explanation of how to efficiently import multiple .sql files into MySQL

MySQL has multiple ways to import multiple .sql f...