美文网首页
Vue加载进度条

Vue加载进度条

作者: 凌康ACG | 来源:发表于2019-12-03 23:18 被阅读0次
    show

    一、nprogress

    路由懒加载会使得我们在第一次打开一个新页面的时候,会有一个加载时间。如果在这个时候我们没有一个提示的话,给人的感觉会是好像我点了页面跳转但是没反应。所以,这个时候我们可以加一个进度条来告知用户。
    https://github.com/rstacruz/nprogress

    二、配置vue项目

    安装

    npm install --save nprogress
    

    使用

    // The Vue build version to load with the `import` command
    // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
    import Vue from 'vue'
    import ElementUI from 'element-ui';
    import 'element-ui/lib/theme-chalk/index.css';
    //进度条
    import NProgress from 'nprogress'
    import 'nprogress/nprogress.css'
    import App from './App'
    import router from './router'
    
    Vue.use(ElementUI);
    Vue.config.productionTip = false
    //进度条
    NProgress.configure({
      easing: 'ease',  // 动画方式
      speed: 500,  // 递增进度条的速度
      showSpinner: false, // 是否显示加载ico
      trickleSpeed: 200, // 自动递增间隔
      minimum: 0.3 // 初始化时的最小百分比
    })
    
    router.beforeEach((to, from , next) => {
      // 每次切换页面时,调用进度条
      NProgress.start();
      // 这个一定要加,没有next()页面不会跳转的。这部分还不清楚的去翻一下官网就明白了
      next();
    })
    router.afterEach(() => {
      // 在即将进入新的页面组件前,关闭掉进度条
      NProgress.done()
    })
    
    /* eslint-disable no-new */
    new Vue({
      el: '#app',
      router,
      components: { App },
      template: '<App/>'
    })
    

    全局定义颜色

    image.png
      /*进度条颜色*/
      #nprogress .bar {
        background: blue !important;
      }
    

    相关文章

      网友评论

          本文标题:Vue加载进度条

          本文链接:https://www.haomeiwen.com/subject/utwhgctx.html