Home > Web Front-end > Vue.js > body text

Getting started with Vue framework: How to obtain singer information through NetEase Cloud API

WBOY
Release: 2023-07-18 12:58:45
Original
1541 people have browsed it

Getting started with the Vue framework: How to obtain singer information through the NetEase Cloud API

Introduction:
Vue.js is a popular JavaScript framework that can be used to build user interfaces. Vue provides a concise and clear way to manage data and render pages, making it easier to develop and maintain web applications. This article will introduce how to obtain singer information through Vue.js and NetEase Cloud API, and provide relevant code examples.

  1. Development environment and preparation
    Before you begin, make sure you have the latest versions of Node.js and Vue CLI installed. If you have not installed Vue CLI, you can run the following command on the command line to install it:
npm install -g @vue/cli
Copy after login

After the installation is complete, you can enter the following command on the command line to create a new Vue project:

vue create music-app
Copy after login

Enter the project directory:

cd music-app
Copy after login
  1. Get access rights to NetEase Cloud API
    To use NetEase Cloud API, we need to obtain access rights first. Open the NetEase Cloud API documentation page (https://binaryify.github.io/NeteaseCloudMusicApi/#/) in your browser, follow the instructions in the document, and obtain the API access authorization.
  2. Create Vue component
    Create a new folder components in the src directory, and create a Vue component file named Singer.vue in the components folder. Singer.vue will be used to display singer information. The code example is as follows:
<template>
  <div>
    <h2>{{ singer.name }}</h2>
    <img :src="singer.avatar" :alt="singer.name" />
    <p>{{ singer.intro }}</p>
  </div>
</template>

<script>
export default {
  name: 'Singer',
  props: {
    id: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      singer: {}
    }
  },
  mounted() {
    this.getSingerInfo()
  },
  methods: {
    getSingerInfo() {
      // 发送API请求获取歌手信息
      // 这里假设我们已经在本地搭建了网易云API的服务器,并且将其部署到了http://localhost:3000/
      const url = `http://localhost:3000/artists/${this.id}`
      fetch(url)
        .then(response => response.json())
        .then(data => {
          this.singer = data
        })
        .catch(error => {
          console.error(error)
        })
    }
  }
}
</script>
Copy after login
  1. Using the Singer component
    Use the Singer component just created in App.vue. The code example is as follows:
<template>
  <div>
    <h1>歌手信息</h1>
    <Singer :id="123" />
  </div>
</template>

<script>
import Singer from './components/Singer.vue'

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

<style>
...
</style>
Copy after login

In the above code, we imported the just created Singer component into App.vue and used the Singer component in the template. We pass an id attribute to the Singer component to uniquely identify the singer's ID. When the Singer component is rendered, the mounted function is called, an API request is sent to obtain the singer information, the obtained data is then saved in the singer variable, and finally displayed in the template.

  1. Compile and run
    After saving all files, run the following command in the command line to compile and run:
npm run serve
Copy after login

Wait for the compilation to complete, the browser will Automatically open the application. You should be able to see a page with artist information.

Summary:
Through the tutorial in this article, we learned how to obtain singer information through Vue.js and NetEase Cloud API. We created a Vue component named Singer and used it in App.vue to display singer information. In the Singer component, we send an API request to the NetEase Cloud API to obtain the singer information and display the data on the page. I wish you success in developing applications using the Vue framework!

The above is the content of the article about getting started with the Vue framework: how to obtain singer information through the NetEase Cloud API. I hope it will be helpful to you.

The above is the detailed content of Getting started with Vue framework: How to obtain singer information through NetEase Cloud API. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!