Common problems in implementing the progress bar function of vue Nprogress

Common problems in implementing the progress bar function of vue Nprogress

NProgress is the progress bar that appears at the top of the browser when the page jumps. Official website: http://ricostacruz.com/nprogress/
github: https://github.com/rstacruz/nprogress

The top progress bar in the picture below is very common, and there is a corresponding plug-in in the vue project. Nprogress

insert image description here

The usage of Nprogress progress bar is as follows:

1. Install the nprogress plugin

npm install --save nprogress
Note that --save here is equivalent to -s , which saves the name and version number of the plug-in to dependencies in the package.json file, so that when others clone the project, they can download all the plug-ins to node_modules through npm install .

2. Use of nprogress plugin

The progress bar here is mainly used in the jump process of page routing, so it can be used directly in router/index.js :

Before the route jumps, start the progress bar loading, and after the route jumps, end the progress bar loading.

The contents of router/index.js file are as follows:

import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store";
import HomeLayout form "@/views/home/layout";
import NProgress from "nprogress";
import userCenter from "./modules/userCenter";
import 'nprogress/nprogress.css'
Vue.use(VueRouter);
NProgress.inc(0.2);
NProgress.configure({easing:'ease',speed:2000,showSpinner:false,trickle:false})
const routes = [{
	path:"/",
	name:"Index",
	redirect:"/index"
},{
	path:"/index",
	name:'Index',
	component:()=>import ('@/views/home/index.vue'),
	meta:{title:'Homepage'}
},{
	path:'/home',
	name:'Home',
	component:()=>import('@/views/home/main'),
	meta:{title:'Homepage'}
},{
	path:'/login',
	name:'Login',
	component:()=>import ('@/views/login'),
	meta:{title:'Login'}
},{
	path:'/register',
	name:'register',
	component:()=>import('@/views/register'),
	meta:{title:'Register'}
},{
	path:'/404',
	name:'404',
	component:()=>import('@/views/errorPage')
},{
	path:'*',
	redirect:'/404'
}]
const router = new VueRouter({
	mode:'history',
	routes
})
//Intercept before routing router.beforeEach((to,from,next)=>{
	//Before the page jumps, start the progress bar NProgress.start();
	//Some interception operations, login permissions, etc...
	const token = window.localStorage.getItem('token'); //Get cache from localstorage if(to.meta.title){
		document.title = to.meta.title; //Change the title of the browser tab to the title of the page
	}
	store.commit('changeCurrentPage',to.path);
	const reg = /[a-zA-Z]+\/$/;
	//Directly redirect to routes that do not require verification if(!to.meta.requireAuth){
		if (reg.test(to.path)){
			next(to.path.replace(reg,''));
			return;
		}
		next();
		return
	}
	if(token&&to.name!=='Index'){
		// Already logged in and the page to be redirected is not the login page if(reg.test(to.path)){
			next(to.path.replace(reg,''));
			return;
		}
		next();//You can jump directly }else if(token && to.name == 'Index'){
		//The page you are logged in and want to jump to is the login page if(reg.test(to.path)){
			next(to.path.replace(reg,''));
			return
		}
		next('/home');//jump directly to the homepage }else if(!token && to.name !='Index'){
		//Not logged in and the page to be jumped is not the login page next('/index');//Jump to the login page }else{
		if (reg.test(to.path)){
			next(to.path.replace(reg,''));
			return;
		}
		next()
	}
})
router.afterEach(to=>{
	NProgress.done();
	window.scrollTo(0,0);
})
//Handle repeated clicks on the current page menu, warning issues const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location){
	return originalPush.call(this,location).catch(err=>err);
}
export default router;

The key points above are as follows:

Introduce plug-ins and corresponding css

insert image description here

nprogress configuration parameter

insert image description here

3. When router.beforeEach intercepts before the route jump, the loading progress bar

insert image description here

4. After router.afterEach route jump ends, close the progress bar

insert image description here

3. nprogress plugin to modify the style

In style of the App.vue file, add the following code to change the color of the progress bar

#nprogress .bar {
  background: #f90 !important; //custom color}

This is the end of this article about the implementation of vue Nprogress progress bar function. For more related vue Nprogress progress bar content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Method for implementing nprogress page loading progress bar in Vue
  • Vue uses nprogress to load the routing progress bar
  • Vue uses nprogress to implement progress bar
  • Vue configures nprogress to implement the progress bar at the top of the page
  • How to use NProgress progress bar in Vue
  • How to use Nprogress.js progress bar in vue project

<<:  MySQL 8.0.17 installation and configuration graphic tutorial

>>:  MySQL 8.0.17 installation graphic tutorial

Recommend

MySQL5.6.31 winx64.zip installation and configuration tutorial

#1. Download # #2. Unzip to local and modify nece...

Example of writing mobile H5 to invoke APP (IOS, Android)

iOS 1. URL scheme This solution is basically for ...

How to process local images dynamically loaded in Vue

Find the problem Today I encountered a problem of...

Solution for Docker Swarm external verification load balancing not taking effect

Problem Description I created three virtual machi...

9 Tips for MySQL Database Optimization

Table of contents 1. Choose the most appropriate ...

Background image cache under IE6

CSS background image flickering bug in IE6 (backg...

Summary of xhtml block level tags

* address - address * blockquote - block quote * c...

CocosCreator Getting Started Tutorial: Network Communication

Network Communication Overview When developing an...

Super detailed steps to install zabbix3.0 on centos7

Preface Recently, part of the company's busin...

How to implement horizontal bar chart with percentage in echarts

Table of contents Example Code Rendering Code Ana...

mysql 5.7.11 winx64.zip installation and configuration method graphic tutorial

Install and configure the MySql database system. ...

Instance method for mysql string concatenation and setting null value

#String concatenation concat(s1,s2); concatenate ...

Docker uses busybox to create a base image

The first line of a Docker image starts with an i...

Why should css be placed in the head tag

Think about it: Why should css be placed in the h...

Get the calculated style in the CSS element (after cascading/final style)

To obtain the calculated style in a CSS element (t...