快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

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

说明

成都创新互联专注于企业网络营销推广、网站重做改版、三元网站定制设计、自适应品牌网站建设、H5场景定制商城网站定制开发、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为三元等各大城市提供网站开发制作服务。

一直想做一个基于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://6mz.cn/article/docicp.html

其他资讯