1、安装
npm install nprogress --save
2、main.js中引入样式
import 'nprogress/nprogress.css'
3、使用
router.js中
//导入
import NProgress from 'nprogress'
router.beforeEach((to, from, next) => {
NProgress.start()
next()
})
router.afterEach(() => {
NProgress.done()
})
或reqeust.js中
//导入
import NProgress from 'nprogress'
service.interceptors.request.use(
config => {
config.headers['accept'] = 'application/json'
NProgress.start()
return config
},
error => {
console.log(error)
return Promise.reject(error)
}
)
service.interceptors.response.use(
response => {
NProgress.done()
const res = response.data
if (res.code !== 200) {
return Promise.reject(new Error(res.message || 'Error'))
} else {
return res
}
},
error => {
if (error.response) { // 登录有问题 后端不返回401,直接报错
Notify({
type: 'danger',
message: error.response.data.message,
duration: 5 * 1000
})
} else {
Notify({
type: 'danger',
message: error.message,
duration: 5 * 1000
})
}
return Promise.reject(error)
}
)
export default service
网友评论