最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue Nprogress進度條功能實現(xiàn)常見問題

 更新時間:2021年07月26日 15:27:27   作者:yehaocheng520  
這篇文章主要介紹了vue Nprogress進度條功能實現(xiàn),NProgress是頁面跳轉(zhuǎn)是出現(xiàn)在瀏覽器頂部的進度條,本文通過實例代碼給大家講解,需要的朋友可以參考下

NProgress是頁面跳轉(zhuǎn)是出現(xiàn)在瀏覽器頂部的進度條
官網(wǎng):http://ricostacruz.com/nprogress/
github:https://github.com/rstacruz/nprogress

下圖中的這種頂部進度條是非常常見的,在vue項目中有對應(yīng)的插件。Nprogress

在這里插入圖片描述

Nprogress進度條的使用方法如下:

1.安裝nprogress插件

npm install --save nprogress
注意此處的--save等同于-s,就是將插件的名稱及版本號保存到package.json文件中的dependencies中,這樣其他人克隆項目后,可以通過npm install就可以下載下來所有的插件到node_modules中了。

2.nprogress插件的使用

此處進度條主要用于頁面路由的跳轉(zhuǎn)過程中,因此可以直接在router/index.js中使用:

在路由跳轉(zhuǎn)之前,開啟進度條加載,在路由跳轉(zhuǎn)之后,結(jié)束進度條的加載。

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:'首頁'}
},{
	path:'/home',
	name:'Home',
	component:()=>import('@/views/home/main'),
	meta:{title:'首頁'}
},{
	path:'/login',
	name:'Login',
	component:()=>import ('@/views/login'),
	meta:{title:'登錄'}
},{
	path:'/register',
	name:'register',
	component:()=>import('@/views/register'),
	meta:{title:'注冊'}
},{
	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)=>{
	//頁面跳轉(zhuǎn)之前,開啟進度條
	NProgress.start();
	//某些攔截操作,是否登錄權(quán)限等...
	const token = window.localStorage.getItem('token');//從localstorage中獲取緩存
	if(to.meta.title){
		document.title = to.meta.title;//將瀏覽器選項卡的標題改為頁面的title
	}
	store.commit('changeCurrentPage',to.path);
	const reg = /[a-zA-Z]+\/$/;
	//不需要校驗的路由直接跳轉(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)的頁面不是登錄頁面
		if(reg.test(to.path)){
			next(to.path.replace(reg,''));
			return;
		}
		next();//可以直接跳轉(zhuǎn)
	}else if(token && to.name == 'Index'){
		//已登錄且要跳轉(zhuǎn)的頁面是登錄頁
		if(reg.test(to.path)){
			next(to.path.replace(reg,''));
			return
		}
		next('/home');//直接跳轉(zhuǎn)到首頁
	}else if(!token && to.name !='Index'){
		//未登錄且要跳轉(zhuǎn)的頁面不是登錄頁
		next('/index');//跳轉(zhuǎn)到登錄頁
	}else{
		if(reg.test(to.path)){
			next(to.path.replace(reg,''));
			return;
		}
		next()
	}
})
router.afterEach(to=>{
	NProgress.done();
	window.scrollTo(0,0);
})
//處理重復(fù)點擊當前頁菜單,出現(xiàn)警告問題
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location){
	return originalPush.call(this,location).catch(err=>err);
}
export default router;

上面的重點如下:

引入插件及對應(yīng)的css

在這里插入圖片描述

nprogress配置參數(shù)

在這里插入圖片描述

3.router.beforeEach路由跳轉(zhuǎn)之前攔截時,加載進度條

在這里插入圖片描述

4.router.afterEach路由跳轉(zhuǎn)結(jié)束后,關(guān)閉進度條

在這里插入圖片描述 

3.nprogress插件修改樣式

App.vue文件中的style樣式中,添加如下代碼,更改進度條的顏色

#nprogress .bar {
  background: #f90 !important; //自定義顏色
}

到此這篇關(guān)于vue Nprogress進度條功能實現(xiàn)的文章就介紹到這了,更多相關(guān)vue Nprogress進度條內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

华池县| 潜江市| 渝中区| 格尔木市| 阿城市| 昭苏县| 城口县| 石河子市| 亳州市| 临高县| 黔东| 扶沟县| 福建省| 英山县| 信宜市| 思茅市| 昌吉市| 迁西县| 岫岩| 潞城市| 拉萨市| 鄢陵县| 高淳县| 六安市| 漳平市| 两当县| 东源县| 阿拉善右旗| 潜江市| 秦皇岛市| 岳普湖县| 天等县| 乳山市| 台州市| 昆明市| 揭东县| 泽普县| 桃江县| 青浦区| 顺昌县| 封丘县|