Vue实现仿音乐播放器5-实现今日推荐访问百度音乐API获取数据
前文
前面已經實現了音樂導航菜單的切換,現在要實現主頁面的今日推薦功能。
效果
實現
實現首頁重定向
當應用初始化時,默認進入主頁面home頁,所以在router下的index.js下配置:
export default new Router({routes: [{path: '/',name: 'Index',redirect:"/home",component: Index,children:[實現在主頁面引入今日推薦組件
首先在components下新建組件Today_recommend.vue組件
<template lang="html"><div class="">今日推薦</div> </template><script>export default {} </script><style lang="css"> </style>然后想在home.vue這個頁面中引入組件Today_recommend.vue,打開home.vue
<template lang="html"><div class=""><TodayRecommend/></div> </template><script> import TodayRecommend from "../components/Today_Recommend" export default {name:"home",components:{TodayRecommend} } </script><style lang="css"> </style>效果
接下來就是完善今日推薦頁面
完善今日推薦頁面
完善樣式
加入scoped范圍限制
<style scoped>.mod-albums {background-color: #fff;padding: 10px 17px; }.hd {display: flex;margin: 14px 0 18px 0; }.hd h2 {-webkit-box-flex: 1;-webkit-flex: 1;flex: 1;margin: 0;padding: 0;font-size: 20px; }.hd div {width: 64px;font-size: 12px;text-align: right; }.mod-albums .gallery {overflow: hidden;margin: 0 -5px; }.mod-albums .gallery .card {width: 33.3%;float: left;-webkit-box-sizing: border-box;box-sizing: border-box;padding: 0 5px 10px; }.mod-albums .gallery .card .album {position: relative; }.mod-albums .gallery .card img {width: 100%;height: auto;border: 1px solid #eee; }.mod-albums .gallery .card .name {font-size: 12px;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;margin-top: 4px;line-height: 14px;max-height: 28px;margin-bottom: 2px; }</style>引入百度音樂接口
具體參照:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/84979978
API接口:
http://tingapi.ting.baidu.com/v1/restserver/ting
配置axios跨域訪問
本地代理配置
打開config下的index.js
? proxyTable: {"/baidu_music_api": {target: "http://tingapi.ting.baidu.com",changeOrigin: true,pathRewrite: {'^/baidu_music_api': ''}}},注:
target:要請求的第三方平接口,這里是百度音樂API :http://tingapi.ting.baidu.com/v1/restserver/ting
changeOrigin: true
在本地會創建一個虛擬服務端,然后發送請求的數據,并同時接收請求的數據,這樣服務端和服務端進行數據的交互就不會有跨域問題。
pathRewrite:路徑重寫
替換target中的請求地址,即別名。
安裝axios
到項目跟目錄下,打開命令行窗口,輸入:
npm install --save axios?
然后重啟項目
入口文件main.js中引入axios
在項目中找到src下的main.js(入口文件)打開
// The Vue build version to load with the `import` command // (runtime-only or standalone) has been set in webpack.base.conf with an alias. import Vue from 'vue' import App from './App' import router from './router' import Axios from "axios"Vue.prototype.$axios? = Axios; Vue.prototype.HOST = "/baidu_music_api" Vue.config.productionTip = false/* eslint-disable no-new */ new Vue({el: '#app',router,components: { App },template: '<App/>' })注:
import Axios from "axios"? 引入axios
Vue.prototype.$axios? = Axios;?? 在vue中使用axios,即掛載axios
?
跨域訪問請求數據
在要實現訪問的頁面,假如加載完就要獲取數據,在,mounted中:
?
mounted(){var url = this.HOST +"/v1/restserver/ting?method=baidu.ting.billboard.billList&type= "+ this.type+"&size=6&offset=0";this.$axios.get(url).then(res => {this.todayRecommend = res.data.song_list}).catch(error => {console.log(error);})}將獲取的數據獲取存放,然后遍歷獲取并顯示數據
完整vue代碼:
<template lang="html"><div class= "mod-albums"><div class="hd log url"><h2>{{title}}</h2><router-link :to="{ name:'MoreList',params:{musictype:this.type,title:title}}" tag= "div">更多</router-link></div><div class="container"><div class="gallery"><div class="scroller"><div class="card url" v-for="(item,index) in todayRecommend" :key= "index"><div class= "album"><img :src="item.pic_big" :alt="item.title"><div class="name">{{ item.title }}</div></div></div></div></div></div></div> </template><P><script> export default{name:"todayRecommend",data(){return{todayRecommend:[]}},props:{title:{type:String,default:"今日榜單"},type:{type:String,default:"1"}},mounted(){var url = this.HOST +"/v1/restserver/ting?method=baidu.ting.billboard.billList&type= "+ this.type+"&size=6&offset=0";this.$axios.get(url).then(res => {this.todayRecommend = res.data.song_list}).catch(error => {console.log(error);})} } </script><style scoped>.mod-albums {background-color: #fff;padding: 10px 17px; }.hd {display: flex;margin: 14px 0 18px 0; }.hd h2 {-webkit-box-flex: 1;-webkit-flex: 1;flex: 1;margin: 0;padding: 0;font-size: 20px; }.hd div {width: 64px;font-size: 12px;text-align: right; }.mod-albums .gallery {overflow: hidden;margin: 0 -5px; }.mod-albums .gallery .card {width: 33.3%;float: left;-webkit-box-sizing: border-box;box-sizing: border-box;padding: 0 5px 10px; }.mod-albums .gallery .card .album {position: relative; }.mod-albums .gallery .card img {width: 100%;height: auto;border: 1px solid #eee; }.mod-albums .gallery .card .name {font-size: 12px;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;margin-top: 4px;line-height: 14px;max-height: 28px;margin-bottom: 2px; }</style>請求數據效果
?
此部分代碼對應分階段代碼中階段二
分階段代碼下載位置:
https://download.csdn.net/download/badao_liumang_qizhi/10846557
總結
以上是生活随笔為你收集整理的Vue实现仿音乐播放器5-实现今日推荐访问百度音乐API获取数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 百度音乐接口使用示例
- 下一篇: Vue实现仿音乐播放器6-实现新歌速递与