vue Nprogress進(jìn)度條功能實(shí)現(xiàn)常見問題
NProgress是頁(yè)面跳轉(zhuǎn)是出現(xiàn)在瀏覽器頂部的進(jìn)度條
官網(wǎng):http://ricostacruz.com/nprogress/
github:https://github.com/rstacruz/nprogress
下圖中的這種頂部進(jìn)度條是非常常見的,在vue
項(xiàng)目中有對(duì)應(yīng)的插件。Nprogress
Nprogress
進(jìn)度條的使用方法如下:
1.安裝nprogress
插件
npm install --save nprogress
注意此處的--save
等同于-s
,就是將插件的名稱及版本號(hào)保存到package.json
文件中的dependencies
中,這樣其他人克隆項(xiàng)目后,可以通過npm install
就可以下載下來所有的插件到node_modules
中了。
2.nprogress
插件的使用
此處進(jìn)度條主要用于頁(yè)面路由的跳轉(zhuǎn)過程中,因此可以直接在router/index.js
中使用:
在路由跳轉(zhuǎn)之前,開啟進(jìn)度條加載,在路由跳轉(zhuǎn)之后,結(jié)束進(jìn)度條的加載。
router/index.js
文件內(nèi)容如下:
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:'首頁(yè)'} },{ path:'/home', name:'Home', component:()=>import('@/views/home/main'), meta:{title:'首頁(yè)'} },{ path:'/login', name:'Login', component:()=>import ('@/views/login'), meta:{title:'登錄'} },{ path:'/register', name:'register', component:()=>import('@/views/register'), meta:{title:'注冊(cè)'} },{ path:'/404', name:'404', component:()=>import('@/views/errorPage') },{ path:'*', redirect:'/404' }] const router = new VueRouter({ mode:'history', routes }) //路由跳轉(zhuǎn)之前做攔截 router.beforeEach((to,from,next)=>{ //頁(yè)面跳轉(zhuǎn)之前,開啟進(jìn)度條 NProgress.start(); //某些攔截操作,是否登錄權(quán)限等... const token = window.localStorage.getItem('token');//從localstorage中獲取緩存 if(to.meta.title){ document.title = to.meta.title;//將瀏覽器選項(xiàng)卡的標(biāo)題改為頁(yè)面的title } store.commit('changeCurrentPage',to.path); const reg = /[a-zA-Z]+\/$/; //不需要校驗(yàn)的路由直接跳轉(zhuǎn) if(!to.meta.requireAuth){ if(reg.test(to.path)){ next(to.path.replace(reg,'')); return; } next(); return } if(token&&to.name!=='Index'){ //已登錄且要跳轉(zhuǎn)的頁(yè)面不是登錄頁(yè)面 if(reg.test(to.path)){ next(to.path.replace(reg,'')); return; } next();//可以直接跳轉(zhuǎn) }else if(token && to.name == 'Index'){ //已登錄且要跳轉(zhuǎn)的頁(yè)面是登錄頁(yè) if(reg.test(to.path)){ next(to.path.replace(reg,'')); return } next('/home');//直接跳轉(zhuǎn)到首頁(yè) }else if(!token && to.name !='Index'){ //未登錄且要跳轉(zhuǎn)的頁(yè)面不是登錄頁(yè) next('/index');//跳轉(zhuǎn)到登錄頁(yè) }else{ if(reg.test(to.path)){ next(to.path.replace(reg,'')); return; } next() } }) router.afterEach(to=>{ NProgress.done(); window.scrollTo(0,0); }) //處理重復(fù)點(diǎn)擊當(dāng)前頁(yè)菜單,出現(xiàn)警告問題 const originalPush = VueRouter.prototype.push; VueRouter.prototype.push = function push(location){ return originalPush.call(this,location).catch(err=>err); } export default router;
上面的重點(diǎn)如下:
引入插件及對(duì)應(yīng)的css
nprogress
配置參數(shù)
3.router.beforeEach
路由跳轉(zhuǎn)之前攔截時(shí),加載進(jìn)度條
4.router.afterEach
路由跳轉(zhuǎn)結(jié)束后,關(guān)閉進(jìn)度條
3.nprogress
插件修改樣式
在App.vue
文件中的style
樣式中,添加如下代碼,更改進(jìn)度條的顏色
#nprogress .bar { background: #f90 !important; //自定義顏色 }
到此這篇關(guān)于vue Nprogress進(jìn)度條功能實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)vue Nprogress進(jìn)度條內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 詳解如何在Vue3+TS的項(xiàng)目中使用NProgress進(jìn)度條
- Vue中nprogress頁(yè)面加載進(jìn)度條的方法實(shí)現(xiàn)
- vue使用nprogress加載路由進(jìn)度條的方法
- vue使用nprogress實(shí)現(xiàn)進(jìn)度條
- vue配置nprogress實(shí)現(xiàn)頁(yè)面頂部進(jìn)度條
- Vue使用NProgress進(jìn)度條的方法
- 在vue項(xiàng)目中使用Nprogress.js進(jìn)度條的方法
- Vue使用NProgress實(shí)現(xiàn)頁(yè)面頂部的進(jìn)度條顯示效果
相關(guān)文章
Vue全局注冊(cè)與局部注冊(cè)兩種組件注冊(cè)的方式
本文主要介紹了Vue全局注冊(cè)與局部注冊(cè)兩種組件注冊(cè)的方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07@vue/cli4.x版本的vue.config.js常用配置方式
這篇文章主要介紹了@vue/cli4.x版本的vue.config.js常用配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05vue3基于elementplus 簡(jiǎn)單實(shí)現(xiàn)表格二次封裝過程
公司渲染表格數(shù)據(jù)時(shí)需要將空數(shù)據(jù)顯示‘-’,并且對(duì)于每一列數(shù)據(jù)的顯示也有一定的要求,基于這個(gè)需求對(duì)element-plus簡(jiǎn)單進(jìn)行了二次封裝,這篇文章主要介紹了vue3基于elementplus 簡(jiǎn)單實(shí)現(xiàn)表格二次封裝過程,需要的朋友可以參考下2024-05-05Vue 設(shè)置axios請(qǐng)求格式為form-data的操作步驟
今天小編就為大家分享一篇Vue 設(shè)置axios請(qǐng)求格式為form-data的操作步驟,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10