Home  >  Article  >  Web Front-end  >  Vue Advanced Tutorial: How to use NetEase Cloud API to implement the song favorites function

Vue Advanced Tutorial: How to use NetEase Cloud API to implement the song favorites function

PHPz
PHPzOriginal
2023-07-18 17:57:161593browse

Vue Advanced Tutorial: How to use NetEase Cloud API to implement the song favorites function

Introduction:
Vue.js is a progressive JavaScript framework for building user interfaces, and NetEase Cloud API is An open network interface that provides numerous music-related functions. This tutorial will teach you how to use Vue.js and NetEase Cloud API to implement a simple song favorites function, allowing users to add, delete and play their favorite music.

Environment preparation:
Before you start, please make sure you have installed Vue.js and axios (a JavaScript library for sending HTTP requests).

Step 1: Obtain NetEase Cloud API permissions
First, we need to apply for a developer account on the NetEase Cloud official website and obtain the appKey and appSecret required for the API. After successful application, you will receive an authorization code (token) for accessing NetEase Cloud API.

Step 2: Create a Vue project
Execute the following command in the command line to create a new Vue project:

vue create music-app

Then enter the project directory and start the development server:

cd music-app
npm run serve

Step 3: Write code
First, we need to create a component named Music.vue to display the music list and operation buttons. Create the Music.vue file in the src/components directory and write the following code in it:

<template>
  <div>
    <ul>
      <li v-for="music in musics" :key="music.id">
        {{ music.name }}
        <button @click="play(music.id)">播放</button>
        <button @click="remove(music.id)">删除</button>
      </li>
    </ul>
    <input type="text" v-model="input" placeholder="歌曲名">
    <button @click="add">添加</button>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      musics: [],
      input: ''
    };
  },
  methods: {
    fetchMusics() {
      axios.get('https://api.music.163.com/v1/song/playlist', {
        headers: {
          'Authorization': 'Bearer ' + this.token
        }
      })
      .then(response => {
        this.musics = response.data;
      })
      .catch(error => {
        console.log(error);
      });
    },
    add() {
      axios.post('https://api.music.163.com/v1/song', {
        name: this.input
      }, {
        headers: {
          'Authorization': 'Bearer ' + this.token
        }
      })
      .then(response => {
        this.input = '';
        this.fetchMusics();
      })
      .catch(error => {
        console.log(error);
      });
    },
    remove(id) {
      axios.delete('https://api.music.163.com/v1/song/' + id, {
        headers: {
          'Authorization': 'Bearer ' + this.token
        }
      })
      .then(response => {
        this.fetchMusics();
      })
      .catch(error => {
        console.log(error);
      });
    },
    play(id) {
      axios.put('https://api.music.163.com/v1/song/' + id, {
        headers: {
          'Authorization': 'Bearer ' + this.token
        }
      })
      .then(response => {
        console.log('正在播放歌曲:' + id);
      })
      .catch(error => {
        console.log(error);
      });
    }
  },
  mounted() {
    this.fetchMusics();
  }
};
</script>

In the above code, we use axios to send HTTP requests, obtain the music list, add music through the NetEase Cloud API, Delete music and play music. Note that you need to replace this.token with your own authorization code.

Step 4: Use Music component
Use Music component in App.vue. Modify the src/App.vue file, the code is as follows:

<template>
  <div id="app">
    <Music></Music>
  </div>
</template>

<script>
import Music from './components/Music.vue';

export default {
  name: 'App',
  components: {
    Music
  }
};
</script>

Now, we have completed the integration of Vue.js and NetEase Cloud API, and can run the project and see the effect.

npm run serve

Visit http://localhost:8080, you will see a song favorites page where you can add, delete and play music.

Summary:
Through this tutorial, we learned how to use Vue.js and NetEase Cloud API to implement a simple song favorites function. In actual projects, you can expand and optimize the code and add more functions according to your needs. I hope this tutorial will be helpful for you to learn Vue.js and use NetEase Cloud API.

The above is the detailed content of Vue Advanced Tutorial: How to use NetEase Cloud API to implement the song favorites function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn