在vue项目中使用Nprogress.js进度条

NProgress是一款在网页顶部添加细长进度条的工具,非常轻巧,使用起来也非常便捷,灵感来源于Google, YouTube。

1、安装

$ npm install --save nprogress 或者
$ yarn add nprogress

//用法
NProgress.start();
NProgress.done();

2、使用

router.js

//导入
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

router.beforeEach((to, from, next) => {
  NProgress.start()
  next()
})

router.afterEach(() => {
  NProgress.done()
})

在路由页面跳转使用,同样在main.js中:

router.beforeEach((to, from, next) => {
if (to.path == '/login') {
    sessionStorage.removeItem('username');
  }
let user = sessionStorage.getItem('username');
if (!user && to.path != '/login') {
    next({path: '/login'})
  } else {
    NProgress.start();
    next()
  }
});

router.afterEach(transition => {
  NProgress.done();
});

广州品牌设计公司https://www.houdianzi.com PPT模板下载大全https://redbox.wode007.com

3.vue使用NProgress 修改颜色

在App.vue中的style中增加:

  <style>
  #nprogress .bar {
      background: red !important; //自定义颜色
    }
</style>
原文地址:https://www.cnblogs.com/qianxiaox/p/14102226.html