页面载入进度条 nprogress的使用

介绍

页面路由切换时,附带一个加载进度条会显得非常友好,不至于白屏时间过长,让用户以为页面假死。

这时候我们可以用到 nprogress[108],在路由切换时开启和关闭:

安装

npm

npm install --save nprogres

yarn

yarn add nprogress

使用

直接调用 start()或者done()来控制进度条。

可以使用inc()随机增长进度条,注意,这个方法永远不会让进度条达到100%。

NProgress.inc()

通过使用done()让进度条关闭。

NProgress.done(true)
import NProgress from nprogress;

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

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

ES6用法(高级用法)

import {
          
    start, done, inc, configure } from nprogress

configure({
          
    easing: ease, speed: 300 })


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

router.afterEach(route => {
          
   
  setTimeout(() => {
          
   
    done() // 进度条加载完成
  }, 300)
})
经验分享 程序员 微信小程序 职场和发展