Home > Web Front-end > Vue.js > Vue technology sharing: How to use NetEase Cloud API to implement the global search function of MV player

Vue technology sharing: How to use NetEase Cloud API to implement the global search function of MV player

WBOY
Release: 2023-07-17 13:09:13
Original
2692 people have browsed it

Vue technology sharing: How to use NetEase Cloud API to implement the global search function of MV player

In the modern music era, the importance of MV (Music Video) has attracted more and more attention. As users, we hope to be able to comprehensively search for our favorite MVs on one platform and play them. This article will introduce how to use the Vue framework and NetEase Cloud API to implement the global search function of a simple MV player.

  1. Preparation
    First of all, before we start, we need to prepare some tools and resources. First, we need to install Vue CLI, a tool for quickly building Vue projects. It can be installed globally with the following command:
npm install -g @vue/cli
Copy after login

Subsequently, we need to create a new Vue project. It can be created in the command line with the following command:

vue create mv-player
Copy after login

After the creation is completed, we enter the project directory and install some necessary dependencies:

cd mv-player
npm install axios
Copy after login

In the project, we also need a Displays the components of the MV player. We can use Element UI, the popular Vue UI framework, to quickly build interfaces. Install Element UI:

vue add element
Copy after login
  1. Get the developer account and key of NetEase Cloud API
    Before using NetEase Cloud API, we need to apply for a developer account and obtain the developer key key. Just register and apply on the NetEase Cloud official website. After the application is completed, we can get information similar to the following:
API地址:http://api.music.163.com
开发者账号:your_account@example.com
开发者密钥:your_developer_key
Copy after login
  1. Create a global search component
    Next, we need to create a global search component to receive user input and send Request to NetEase Cloud API for search. In the src directory, create a components folder and create a SearchBar.vue file in it. In this file, we can implement the logic of global search.

First, we need to introduce axios to send HTTP requests. In the script section of the component, add the following code:

import axios from 'axios'

export default {
  name: 'SearchBar',
  data() {
    return {
      keyword: ''
    }
  },
  methods: {
    search() {
      axios.get('http://api.music.163.com/search', {
        params: {
          keywords: this.keyword
        }
      })
      .then(response => {
        // 处理搜索结果
      })
      .catch(error => {
        console.error(error)
      })
    }
  }
}
Copy after login

In the above code, we define a data attribute to store the keywords entered by the user. In the search method, we use axios to send a GET request to the search interface of NetEase Cloud API and pass keywords as parameters. In the then callback, we can handle the search results returned by the API.

Next, we need to add a text input box and a search button to the component's template. In the template section, add the following code:

<template>
  <div>
    <input v-model="keyword" type="text" placeholder="请输入关键字" />
    <button @click="search">搜索</button>
  </div>
</template>
Copy after login

In the above code, we use the v-model directive to bind the keywords entered by the user with the data attribute of the component. When the user clicks the search button, the search method is called.

Finally, we add some basic styles to the component's style. You can use the styles provided by Element UI to quickly beautify components. In the style section, add the following code:

<style scoped>
input {
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
}

button {
  margin-left: 10px;
  padding: 10px;
  background-color: #409EFF;
  color: #fff;
  border-radius: 4px;
}
</style>
Copy after login

At this point, we have completed writing the global search component.

  1. Using the global search component in the App component
    Next, we need to use the global search component in the App component and display the search results. In the src directory, open the App.vue file. First, introduce the global search component:
import SearchBar from './components/SearchBar.vue'
Copy after login

Then, in the template part of the component, add the following code:

<template>
  <div class="app">
    <SearchBar></SearchBar>
    <div v-for="mv in mvs" :key="mv.id">
      <img :src="mv.cover" alt="mv cover" />
      <span>{{ mv.name }}</span>
      <span>{{ mv.artist }}</span>
    </div>
  </div>
</template>
Copy after login

In the above code, we use the v-for directive to Traverse the mvs array, which is used to store search results. In each search result, we display the cover, name and artist information of the MV.

Then, in the script section of the component, add the following code:

export default {
  name: 'App',
  components: {
    SearchBar
  },
  data() {
    return {
      mvs: []
    }
  }
}
Copy after login

In the above code, we define a data attribute mvs for storing search results.

Next, in the search method of the global search component, we can process the search results and save them to the mvs attribute of the App component. The code to modify the global search component is as follows:

import axios from 'axios'

export default {
  name: 'SearchBar',
  data() {
    return {
      keyword: ''
    }
  },
  methods: {
    search() {
      axios.get('http://api.music.163.com/search', {
        params: {
          keywords: this.keyword
        }
      })
      .then(response => {
        this.$emit('search', response.data.result.mvs)
      })
      .catch(error => {
        console.error(error)
      })
    }
  }
}
Copy after login

In the above code, we pass the search results to the parent component through this.$emit. In the App component, we add a method that listens to this event and saves the search results to the mvs attribute. Modify the code of the App component as follows:

export default {
  name: 'App',
  components: {
    SearchBar
  },
  data() {
    return {
      mvs: []
    }
  },
  methods: {
    handleSearchResult(mvs) {
      this.mvs = mvs
    }
  }
}
Copy after login

Finally, in the template of the global search component, add a search event listener for the SearchBar component and call the corresponding method in the parent component. Modify the code of the global search component as follows:

<template>
  <div>
    <input v-model="keyword" type="text" placeholder="请输入关键字" />
    <button @click="search">搜索</button>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'SearchBar',
  data() {
    return {
      keyword: ''
    }
  },
  methods: {
    search() {
      axios.get('http://api.music.163.com/search', {
        params: {
          keywords: this.keyword
        }
      })
      .then(response =&gt; {
        this.$emit('search', response.data.result.mvs)
      })
      .catch(error =&gt; {
        console.error(error)
      })
    }
  }
}
</script>
Copy after login

Now, we have completed the implementation of the global search function of the MV player. By entering keywords in the global search component and clicking the search button, the search results can be displayed.

To sum up, this article introduces how to use the Vue framework and NetEase Cloud API to implement the global search function of the MV player. By writing a global search component, we can easily send search requests to NetEase Cloud API and display search results. I hope this article will help you learn Vue technology.

The above is the detailed content of Vue technology sharing: How to use NetEase Cloud API to implement the global search function of MV player. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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