Vue3 encapsulation message prompt instance function
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 directivesimport 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' }) SummarizeThis 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:
|
<<: Reasons for the sudden drop in MySQL performance
>>: Implementation of Docker deployment of MySQL cluster
Previously, I introduced the use of the charAt() ...
This article example shares the specific code for...
Text Shadow text-shadow: horizontal offset vertic...
Preface When we write web pages, we will definite...
Docker installation Install dependency packages s...
This article shares a native JS implementation of...
There has been a lot of discussion about H1 recent...
mysql download, install and configure 5.7.20 / 5....
Copy code The code is as follows: <html> &l...
First execute the command: [root@mini61 setuptool...
Table of contents 1. Introduction 2. Configuratio...
Pure CSS3 makes a butterfly flapping its wings, s...
Preface In the previous article Two data types in...
This article shares the specific code for the WeC...
Effect: <!doctype html> <html> <he...