Home  >  Article  >  Web Front-end  >  Vue from beginner to proficient: How to use NetEase Cloud API to get popular music list

Vue from beginner to proficient: How to use NetEase Cloud API to get popular music list

WBOY
WBOYOriginal
2023-07-17 09:33:091347browse

Vue from beginner to proficient: How to use NetEase Cloud API to obtain popular music lists

Introduction:
Vue.js, as a popular JavaScript framework, is widely used in front-end development. Combining Vue.js and NetEase Cloud API, we can easily implement the function of obtaining popular music lists. This article will introduce in detail how to use Vue.js and NetEase Cloud API to quickly implement the function of obtaining popular music lists, and give corresponding code examples.

  1. Preparation
    Before we start, we need to prepare some basic working environment and files.
    First, make sure Node.js is installed on your computer. You can enter the following command on the command line to check whether the installation is successful:

    node -v

    Secondly, create a new Vue project. Enter the following command on the command line to create a new Vue project:

    vue create music-app

    Enter the project directory and install axios for sending HTTP requests:

    cd music-app
    npm install axios --save
  2. Get NetEase Cloud Music API Token
    Before using the NetEase Cloud API, we need to obtain a valid Token. Open the browser, enter the NetEase Cloud Developer Platform, register a new account and log in.

After logging in successfully, click "Management Console" and find the "Create Application" option. Follow the instructions to fill in the app name and description and click "Create App".

After successful creation, you will get an App Key and App Secret. Save these two information, we will use them later.

  1. Get popular music list interface
    In the "Management Console" of the NetEase Cloud Developer Platform, find "API Documentation" and click "Music API". Select "Rankings" on the left navigation bar, then click "Cloud Music Hot Songs List".

In the interface document, we can find the interface information for obtaining the popular music list. Record the URL and request parameters of the interface, we will use them later.

  1. Create Vue components
    Create a new folder components in the src directory of the project, and create a new file MusicList.vue in the components folder.

In MusicList.vue, we will implement the function of getting the popular music list. First, we import the axios module and define the component's data and methods.

<template>
  <div>
    <h1>热门音乐列表</h1>
    <div v-for="music in musics" :key="music.id">
      {{ music.name }}
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      musics: []
    };
  },
  methods: {
    getMusicList() {
      const url = 'https://api.music.163.com/top/list';
      const params = {
        idx: 1,
        limit: 10,
        format: 'json'
      };

      axios.get(url, {params})
        .then(response => {
          this.musics = response.data.playlist.tracks;
        })
        .catch(error => {
          console.log(error);
        });
    }
  },
  created() {
    this.getMusicList();
  }
};
</script>
  1. Using components in App.vue
    Open the App.vue file and introduce the MusicList component we just created:

    <template>
      <div id="app">
     <MusicList />
      </div>
    </template>
    
    <script>
    import MusicList from './components/MusicList.vue';
    
    export default {
      components: {
     MusicList
      }
    };
    </script>
  2. Run the project
    Enter the following command in the command line to run our project:

    npm run serve

    Open the browser and visit http://localhost:8080, you will see a list of popular music.

Conclusion:
This article introduces how to use Vue.js and NetEase Cloud API to obtain the popular music list. Through this example, you can further understand the basic syntax and use of components of Vue.js, and how to obtain data by sending HTTP requests.

I hope this article will be helpful to you in learning Vue.js and network API. You are welcome to apply this knowledge in actual development. I wish you become a senior developer of Vue.js!

The above is the detailed content of Vue from beginner to proficient: How to use NetEase Cloud API to get popular music list. 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