Implementation tutorial of Vue progress bar component (code example)
The content of this article is about the implementation tutorial (code example) of the Vue progress bar component. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Recently, in a personal project, I wanted to optimize the process of jumping between pages. I thought of the page jump progress bar used in many documents or npm, so I wanted to implement one myself. This is recorded.
Let’s take a look at the effect of npm searching for components:
so let’s implement it together.
Definition of usage
If you want to implement a component, you must think about your needs and define a comfortable usage method yourself. There is also a principle. For users, the simpler the usage, the better. The corresponding price is that the complexity of writing this component will become higher.
The way I want to use it is this: this method can be called anywhere and its status can be controlled at any time.
Look at the corresponding code:
async someFunction () { this.$progress.start() try { const ret = await axios.get('/xxx') // some code ... this.$progress.finish() } catch (err) { // cache err ... this.$progress.fail() } }
Of course, if you want to use it anywhere, one step is indispensable, which is to register this component globally:
import progressBar from 'xxx/progress-bar' Vue.use(progressBar, { // some config ... })
If you don’t want to register it globally, you can also use it separately within a component, as long as you are happy.
Implementation process
First create a folder and two files:
progress-bar - 01 progress-bar.vue - 02 index.js
Open progress-bar.vue, first To write the structure and style, it’s very simple here:
<template> <p></p> </template> <style> .bar { position: fixed; z-index: 99999; opacity: 1; } </style>
When registering the component, I want to customize some functions, such as
-
Success color
Failure color
Progress bar position
Animation Transition time
Loading direction
Height
Can it be completed automatically
Of course, you can add whatever you think of, then these customizable attributes will naturally become the props of the component:
export default { name: 'progressBar', props: { options: { type: Object, default () { return { succColor: 'rgb(76, 164, 214)', failColor: 'rgb(218, 26, 101)', position: 'top', transition: { widthSpeed: 200, opacitySpeed: 400, duration: 300 // 定义消失时间 ms }, inverse: false, // 进度条的加载方向 thickness: 2 // 进度条的高度 } } } } }
In addition to the attributes to be defined , then the component itself must always have some properties of its own to control its own state. For example, with this component, you need to control the length, display and hiding of the progress bar.
Add the data attribute of vue:
data () { return { percent: 0, // 进度条长度 show: false, // 显示和隐藏 canSuccess: true // 是否是成功的状态 } }
With these attributes, the progress bar will "move by itself" according to changes in these attributes. So the first thing that comes to mind at this time is of course Vue's computed properties:
computed: { style () { // 先拿到乱七八糟的属性 const { succColor, failColor, location, transition, inverse, thickness } = this.options const { widthSpeed, opacitySpeed } = transition const { canSuccess, preset, show } = this // 定义 css 样式 const style = { backgroundColor: canSuccess ? succColor : failColor, opacity: show ? 1 : 0 } if (position !== 'top' || position !== 'bottom') { throw new Error('The wrong config of position!') } style[position] = 0 if (inverse) { style.left = 0 } else { style.right = 0 } style.width = preset + '%' // 设置进度条长度 style.height = thickness + 'px' // 设置显示高度 style.transition = `width ${widthSpeed}ms, opacity ${opacitySpeed}ms` // 设置过度样式 return style } }
The vue component is actually completed here, and the next step is how to control it. Open index.js, and first write a standard component format:
import progressBar from './progress-bar.vue' export default { install (Vue, options = {}) { // 注册组件 Vue.component(progressBar.name, progressBar) } }
After that, we will use the extension method provided by Vue to complete our needs.
The first step is to add the autoFinish attribute to set whether the animation can be completed automatically. The default is true. Of course, if a route or request is always in the pending state, you can set it to last forever. Actions that don't complete the animation.
The second step is to write an object that contains start, finish, fail methods and animation code.
The third step is to hang this object on the prototype of Vue
The complete code and description are as follows:
import progressBar from './progress-bar.vue' export default { install (Vue, options = {}) { // 注册组件 Vue.component(progressBar.name, progressBar) // 创建一个 Vue 子类 const Component = Vue.extend(progressBar) // 拿到自定义的属性 const { autoFinish, ...res } = options // 创建组件实例 const vm = new Component({ data: { autoFinish: typeof autoFinish === 'boolean' ? autoFinish : true } }) // 将 progressBar 的默认 options 与 自定义的 options 合并 options = Object.assign(vm.$props.options, { ...res }) //合并新的 props vm.$propsData = options vm.$mount() // 如果是服务端渲染那么不继续执行 if (!vm.$isServer) { document.body.appendChild(vm.$el) } let timer = null const progress = { start () { if (Vue.$isServer) return // 每次调用 start 都重新初始化一次,比如多次点击某个按钮连续请求,那么每次都从0开始 vm.percent = 0 vm.show = true vm.canSuccess = true // 定一个增量,这个值可以改成参数,也可以按照使用经验来设定 const CUT_SCALE = 5 // 定义每 100 秒来执行一次动画 timer = setInterval(() => { // 每次执行增量动画 this.increase((CUT_SCALE - 1) * Math.random() + 1) // 如果进度大于 95%,并且设置了自动完成,那么执行结束动作 if (vm.percent > 95 && vm.autoFinish) { this.finish() } }, 100) }, increase (cut) { vm.percent = Math.min(99, vm.percent + cut) }, hide () { clearInterval(timer) // 这里面有2个定时器,外层定时器是让用户可以看到这个 进度已经完成啦 // 内层定时器,由于 opacity 消失需要一定的过渡时间,所以要等待它消失以后 // 在将其进度设置为0,等待下次调用,如果不延迟,那么会看到进度到100后又回到0的动画 setTimeout(() => { vm.show = false setTimeout(() => { vm.percent = 0 timer = null }, vm.options.transition.opacitySpeed) }, vm.options.transition.duration) }, // 下面这2个方法就很简单了,只需要完成进度,然后执行隐藏即可 finish () { if (Vue.$isServer) return vm.percent = 100 this.hide() }, fail () { if (Vue.$isServer) return // 修改未成功的状态,实际效果就是改变最后的颜色 vm.canSuccess = false vm.percent = 100 this.hide() } } // 最后挂在到全局 Vue.prototype.$progress = progress } }
Here, a progress The bar component is complete. You can practice it yourself, start a project, use vue-router's beforeResolve statement cycle hook, or write a timer to simulate asynchronousness to test it.
The above is the detailed content of Implementation tutorial of Vue progress bar component (code example). For more information, please follow other related articles on the PHP Chinese website!

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.