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

Exploring the practical value of the CSS property *-gradient

Let me first introduce an interesting property - ...

How to run Spring Boot application in Docker

In the past few days, I have studied how to run s...

Web designers should optimize web pages from three aspects

<br />With the increase of bandwidth, there ...

Detailed tutorial for springcloud alibaba nacos linux configuration

First download the compressed package of nacos fr...

Vue implements simple calculator function

This article example shares the specific code of ...

Summary of knowledge points about covering index in MySQL

If an index contains (or covers) the values ​​of ...

HTML background color gradient achieved through CSS

Effect screenshots: Implementation code: Copy code...

mysql error number 1129 solution

SQLyog connects to mysql error number 1129: mysql...

Sample code for implementing rolling updates of services using Docker Swarm

1. What is Docker Swarm? Docker Swarm is a cluste...

Detailed explanation of Vue component reuse and expansion

Table of contents Overview Is the extension neces...

How to find websites with SQL injection (must read)

Method 1: Use Google advanced search, for example...

In-depth explanation of MySQL learning engine, explain and permissions

engine Introduction Innodb engine The Innodb engi...

Install Docker on Centos7 (2020 latest version available, just copy and paste)

Refer to the official documentation here for oper...

HTML tag full name and function introduction

Alphabetical DTD: Indicates in which XHTML 1.0 DT...