vue模仿网易云音乐的单页面应用-创新互联
说明

一直想做一个基于VUE的项目,但是因为项目往往要涉及到后端的知识(不会后端真的苦),所以就没有一直真正的动手去做一个项目。
直到发现GitHub上有网易云音乐的api NeteaseCloudMusicApi ,才开始动手去做。
仅仅完成了首页,登入,歌单,歌曲列表页。
项目地址
https://github.com/qp97vi/music
项目成功运行还要把后端api在本地运行
前端技术栈
vue2+vuex+vue-router+axios+mint-ui+webpack
遇到的问题
1.前端路由拦截
想做一个登录拦截,验证没有登录之前,就跳转到登录页面
解决方法是:通过http response 拦截器判断token(本地的cookie)判断
创建一个http.js
import axios from 'axios'
import store from './store/store'
import * as types from './store/types'
import router from './router/index'
// axios 配置
axios.defaults.timeout = 5000
axios.defaults.baseURL = 'https://api.github.com'
// http request 拦截器
axios.interceptors.request.use(
config => {
if (store.state.xsrfCookieName) {
config.headers.Authorization = `xsrfCookieName ${store.state.xsrfCookieName}`
}
return config
},
err => {
return Promise.reject(err)
},
)
// http response 拦截器
axios.interceptors.response.use(
response => {
return response
},
error => {
if (error.response) {
switch (error.response.status) {
case 401:
// 401 清除token信息并跳转到登录页面
store.commit(types.LOGOUT)
// 只有在当前路由不是登录页面才跳转
router.currentRoute.path !== 'login' &&
router.replace({
path: 'login',
query: { redirect: router.currentRoute.path },
})
}
}
// console.log(JSON.stringify(error));//console : Error: Request failed with status code 402
return Promise.reject(error.response.data)
},
)
export default axios 网站名称:vue模仿网易云音乐的单页面应用-创新互联
网页URL:http://www.jxjierui.cn/article/docicp.html


咨询
建站咨询
