網易雲音樂API,的調用方法 ,vue項目中(在本地使用)

1. 在cmd 命令行下:安裝並啓動:

git  clone   https://github.com/Binaryify/NeteaseCloudMusicApi.git  /* 下載 */

cd NeteaseCloudMusicApi /* 進入項目的根目錄*/

yarn 或者 npm install  /*安裝依賴*/

node app.js   /* 運行項目,啓動該 */

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
上面步驟執行完成,出現下面的字樣,代表已成功安裝;
在這裏插入圖片描述
運行,運行成功:
在這裏插入圖片描述
這裏的 " http://localhost:3000 " 就是我們接下來要請求的網易雲音樂API 的 服務器地址了,注意這裏的端口可改,更多使用參考文章後的鏈接。

2. 在vue項目中調用它的接口:

src下新建一個plugins的文件夾,在該文件夾下新建axios.js文件 ( 項目中已用 yarn add axios 安裝了axios插件);

axios.js文件 (這裏關於axios的寫法不做多的說明)

import axios from 'axios'
import qs from 'qs'

axios.defaults.withCredentials = true// 允許跨域設置,不然可能因爲拿不到cookie而報錯

axios.defaults.baseURL = 'http://localhost:3000/'   /*這裏的地址就是剛剛啓起來的服務器地址  */

/*請求攔截*/
axios.interceptors.request.use(
  config => {
    if (config.meth === 'post' && !(config.data instanceof FormData)) {
      config.headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
      config.data = qs.stringify(config.data, { arrayFormat: 'repeat' }) /*這裏是,後端要求傳數組的時候做的設置,以前出過錯*/
    }
    return config
  }, error => {
    return Promise.reject(error)
  }
)
/* 響應攔截 */
axios.interceptors.response.use(
  res => {
   /*可在這裏根據返回的狀態碼做一些攔截操作*/
    return res
  }, err => {
    return Promise.resolve(err)
  }
)
export default axios  /*記得導出*/

在組件中測試調用:
about.vue

<template>
  <div class="about">
    <el-button @click="getWangyi">獲取熱門歌單</el-button>
  </div>
</template>

<script>
import axios from '@/plugins/axios.js' /*引入封裝的axios*/

export default {
  methods: {
    getWangyi () {
      axios({
        url: '/playlist/hot',  /*熱門歌單接口地址*/
        method: 'post'
      })
        .then(res => {
          console.log("我拿到的數據:", res.data.tags)
        })
        .catch(err => {
          console.log(err)
        })
    }
  }
}
</script>

在這裏插入圖片描述

網易雲音樂API 地址 : https://binaryify.github.io/NeteaseCloudMusicApi/#/

它的GitHub地址: https://github.com/Binaryify/NeteaseCloudMusicApi

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章