手机登录/注册
X
登录
https://ricostacruz.com/nprogress
npm i -S nprogress
main.ts入口文件引入 nprogress
import NProgress from 'nprogress' // 引入nprogress插件
import 'nprogress/nprogress.css' // 这个nprogress样式必须引入
axios请求
// axios请求拦截器
axios.interceptors.request.use(
config => {
NProgress.start() // 设置加载进度条(开始..)
return config
},
error => {
return Promise.reject(error)
}
)
// axios响应拦截器
axios.interceptors.response.use(
function(response) {
NProgress.done() // 设置加载进度条(结束..)
return response
},
function(error) {
return Promise.reject(error)
}
)
切换路由
//导入
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
router.beforeEach((to, from, next) => {
NProgress.start()
next()
})
router.afterEach(() => {
NProgress.done()
})
首屏
<script>
NProgress.start()
// 给页面设置请求加载进度条效果
window.onload = function() {
NProgress.done()
}
</script>
nuxt3
import NProgress from "nprogress";
import "nprogress/nprogress.css";
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.hook("app:mounted", (e: any) => {
console.log(11111111);
});
nuxtApp.hook("page:start", (e: any) => {
NProgress.start();
});
nuxtApp.hook("page:finish", (e: any) => {
setTimeout(() => {
NProgress.done();
}, 150);
});
nuxtApp.hook("app:error", (e: any) => {
if (process.client) {
NProgress.done();
}
});
});