Home >Web Front-end >Vue.js >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 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.
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
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.
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.
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>
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>
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!