說明

一直想做一個(gè)基于VUE的項(xiàng)目,但是因?yàn)轫?xiàng)目往往要涉及到后端的知識(shí)(不會(huì)后端真的苦),所以就沒有一直真正的動(dòng)手去做一個(gè)項(xiàng)目。
直到發(fā)現(xiàn)GitHub上有網(wǎng)易云音樂的api NeteaseCloudMusicApi ,才開始動(dòng)手去做。
僅僅完成了首頁,登入,歌單,歌曲列表頁。
項(xiàng)目地址
https://github.com/qp97vi/music
項(xiàng)目成功運(yùn)行還要把后端api在本地運(yùn)行
前端技術(shù)棧
vue2+vuex+vue-router+axios+mint-ui+webpack
遇到的問題
1.前端路由攔截
想做一個(gè)登錄攔截,驗(yàn)證沒有登錄之前,就跳轉(zhuǎn)到登錄頁面
解決方法是:通過http response 攔截器判斷token(本地的cookie)判斷
創(chuàng)建一個(gè)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信息并跳轉(zhuǎn)到登錄頁面
store.commit(types.LOGOUT)
// 只有在當(dāng)前路由不是登錄頁面才跳轉(zhuǎn)
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模仿網(wǎng)易云音樂的單頁面應(yīng)用-創(chuàng)新互聯(lián)
瀏覽地址:http://www.chinadenli.net/article28/docicp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、App設(shè)計(jì)、自適應(yīng)網(wǎng)站、網(wǎng)站收錄、網(wǎng)站設(shè)計(jì)、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容