Detailed explanation of use cases of vue+toast pop-up component
This time I will bring you a detailed explanation of the use cases of the vue toast pop-up component. What are the precautions when using the vue toast pop-up component? Here are the actual cases, let's take a look.
I believe that everyone can write ordinary vue components, definition->introduction->registration->use, everything is done in one go, but what if we want to customize a pop-up component today?
First, let’s analyze the characteristics (requirements) of the pop-up component:
0. Lightweight--a component is less than 1Kib (actual packaging is less than 0.8k)
1 . Generally used in multiple places - need to solve the repeated reference registration on each page
1. Generally interact with js - no need to write in <toast :show ="true" text="Pop-up message"></toast>
Today, we will implement a toast pop-up component based on vue based on the above two requirements. The picture below is the final rendering.
1. First write an ordinary vue component
File location/src/toast/toast.vue
<template> <p class="wrap">我是弹窗</p> </template> <style scoped> .wrap{ position: fixed; left: 50%; top:50%; background: rgba(0,0,0,.35); padding: 10px; border-radius: 5px; transform: translate(-50%,-50%); color:#fff; } </style>
2. Introduce components into the pages we need to use to facilitate viewing effects and errors
<template> <p id="app"> <toast></toast> </p> </template> <script> import toast from './toast/toast' export default { components: {toast}, } </script>
3. Implement dynamic loading of components
You can see that a static pop-up layer has been displayed. Next, let's take a look at how to implement dynamic pop-up.
We first create a new index.js under the /src/toast/ directory, and then enter the following code in index.js (because of this code The coupling is quite serious, so I won’t explain it line by line and change it to inline comments)
File location/src/toast/index.js
import vue from 'vue' // 这里就是我们刚刚创建的那个静态组件 import toastComponent from './toast.vue' // 返回一个 扩展实例构造器 const ToastConstructor = vue.extend(toastComponent) // 定义弹出组件的函数 接收2个参数, 要显示的文本 和 显示时间 function showToast(text, duration = 2000) { // 实例化一个 toast.vue const toastDom = new ToastConstructor({ el: document.createElement('p'), data() { return { text:text, show:true } } }) // 把 实例化的 toast.vue 添加到 body 里 document.body.appendChild(toastDom.$el) // 过了 duration 时间后隐藏 setTimeout(() => {toastDom.show = false} ,duration) } // 注册为全局组件的函数 function registryToast() { // 将组件注册到 vue 的 原型链里去, // 这样就可以在所有 vue 的实例里面使用 this.$toast() vue.prototype.$toast = showToast }
export default registryToast
Attach a portal vue.extend official document
4. Trial
At this point, we have initially completed a global registration and Let’s try out the dynamically loaded toast component.
Register the component in vue’s entry file (if scaffolding is generated, it is ./src/main.js). File location/src/main.js
import toastRegistry from './toast/index' // 这里也可以直接执行 toastRegistry() Vue.use(toastRegistry) 我们稍微修改一下使用方式,把 第二步 的引用静态组件的代码,改成如下 <template> <p id="app"> <input type="button" value="显示弹窗" @click="showToast"> </p> </template> <script> export default { methods: { showToast () { this.$toast('我是弹出消息') } } } </script>
##5. OptimizationNow we have initially implemented a pop-up window. However, it is still a little short of success because it lacks an animation. , the current pop-up and hiding are very stiff.
Let’s slightly modify the showToast function in toast/index.js (there are changes in the commented areas)
File location /src/toast/index.js
function showToast(text, duration = 2000) { const toastDom = new ToastConstructor({ el: document.createElement('p'), data() { return { text:text, showWrap:true, // 是否显示组件 showContent:true // 作用:在隐藏组件之前,显示隐藏动画 } } }) document.body.appendChild(toastDom.$el) // 提前 250ms 执行淡出动画(因为我们再css里面设置的隐藏动画持续是250ms) setTimeout(() => {toastDom.showContent = false} ,duration - 1250) // 过了 duration 时间后隐藏整个组件 setTimeout(() => {toastDom.showWrap = false} ,duration) }
Then, modify the style of toast.vue
File location/src/toast/toast.vue
<template> <p class="wrap" v-if="showWrap" :class="showContent ?'fadein':'fadeout'">{{text}}</p> </template> <style scoped> .wrap{ position: fixed; left: 50%; top:50%; background: rgba(0,0,0,.35); padding: 10px; border-radius: 5px; transform: translate(-50%,-50%); color:#fff; } .fadein { animation: animate_in 0.25s; } .fadeout { animation: animate_out 0.25s; opacity: 0; } @keyframes animate_in { 0% { opacity: 0; } 100%{ opacity: 1; } } @keyframes animate_out { 0% { opacity: 1; } 100%{ opacity: 0; } } </style>
You’re done. A toast component is initially completed
Summary
- vue.extend function can To generate a component constructor, you can use this function to construct a vue component instance
- You can use document.body.appendChild() to dynamically add the component to the body
- vue.prototype.$toast = showToast You can register the component globally
- Displaying animation is relatively simple, hiding animation must reserve enough animation execution before hiding Time
- The source code address of this article is here
- The above are not important, the important thing is to give this article a star
- I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Detailed explanation of jQuery element selector use cases
The above is the detailed content of Detailed explanation of use cases of vue+toast pop-up component. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of the mode function in C++ In statistics, the mode refers to the value that appears most frequently in a set of data. In C++ language, we can find the mode in any set of data by writing a mode function. The mode function can be implemented in many different ways, two of the commonly used methods will be introduced in detail below. The first method is to use a hash table to count the number of occurrences of each number. First, we need to define a hash table with each number as the key and the number of occurrences as the value. Then, for a given data set, we run

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

Detailed explanation of the remainder function in C++ In C++, the remainder operator (%) is used to calculate the remainder of the division of two numbers. It is a binary operator whose operands can be any integer type (including char, short, int, long, etc.) or a floating-point number type (such as float, double). The remainder operator returns a result with the same sign as the dividend. For example, for the remainder operation of integers, we can use the following code to implement: inta=10;intb=3;

Detailed explanation of the usage of Vue.nextTick function and its application in asynchronous updates. In Vue development, we often encounter situations where data needs to be updated asynchronously. For example, data needs to be updated immediately after modifying the DOM or related operations need to be performed immediately after the data is updated. The .nextTick function provided by Vue emerged to solve this type of problem. This article will introduce the usage of the Vue.nextTick function in detail, and combine it with code examples to illustrate its application in asynchronous updates. 1. Vue.nex

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

Detailed explanation of Linux's curl command Summary: curl is a powerful command line tool used for data communication with the server. This article will introduce the basic usage of the curl command and provide actual code examples to help readers better understand and apply the command. 1. What is curl? curl is a command line tool used to send and receive various network requests. It supports multiple protocols, such as HTTP, FTP, TELNET, etc., and provides rich functions, such as file upload, file download, data transmission, proxy
