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

Jul 17, 2023 pm 01:09 PM
vue NetEase Cloud API global search

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use echarts in vue How to use echarts in vue May 09, 2024 pm 04:24 PM

Using ECharts in Vue makes it easy to add data visualization capabilities to your application. Specific steps include: installing ECharts and Vue ECharts packages, introducing ECharts, creating chart components, configuring options, using chart components, making charts responsive to Vue data, adding interactive features, and using advanced usage.

The role of export default in vue The role of export default in vue May 09, 2024 pm 06:48 PM

Question: What is the role of export default in Vue? Detailed description: export default defines the default export of the component. When importing, components are automatically imported. Simplify the import process, improve clarity and prevent conflicts. Commonly used for exporting individual components, using both named and default exports, and registering global components.

How to use map function in vue How to use map function in vue May 09, 2024 pm 06:54 PM

The Vue.js map function is a built-in higher-order function that creates a new array where each element is the transformed result of each element in the original array. The syntax is map(callbackFn), where callbackFn receives each element in the array as the first argument, optionally the index as the second argument, and returns a value. The map function does not change the original array.

The difference between event and $event in vue The difference between event and $event in vue May 08, 2024 pm 04:42 PM

In Vue.js, event is a native JavaScript event triggered by the browser, while $event is a Vue-specific abstract event object used in Vue components. It is generally more convenient to use $event because it is formatted and enhanced to support data binding. Use event when you need to access specific functionality of the native event object.

The role of onmounted in vue The role of onmounted in vue May 09, 2024 pm 02:51 PM

onMounted is a component mounting life cycle hook in Vue. Its function is to perform initialization operations after the component is mounted to the DOM, such as obtaining references to DOM elements, setting data, sending HTTP requests, registering event listeners, etc. It is only called once when the component is mounted. If you need to perform operations after the component is updated or before it is destroyed, you can use other lifecycle hooks.

The difference between export and export default in vue The difference between export and export default in vue May 08, 2024 pm 05:27 PM

There are two ways to export modules in Vue.js: export and export default. export is used to export named entities and requires the use of curly braces; export default is used to export default entities and does not require curly braces. When importing, entities exported by export need to use their names, while entities exported by export default can be used implicitly. It is recommended to use export default for modules that need to be imported multiple times, and use export for modules that are only exported once.

What are hooks in vue What are hooks in vue May 09, 2024 pm 06:33 PM

Vue hooks are callback functions that perform actions on specific events or lifecycle stages. They include life cycle hooks (such as beforeCreate, mounted, beforeDestroy), event handling hooks (such as click, input, keydown) and custom hooks. Hooks enhance component control, respond to component life cycles, handle user interactions and improve component reusability. To use hooks, just define the hook function, execute the logic and return an optional value.

What scenarios can event modifiers in vue be used for? What scenarios can event modifiers in vue be used for? May 09, 2024 pm 02:33 PM

Vue.js event modifiers are used to add specific behaviors, including: preventing default behavior (.prevent) stopping event bubbling (.stop) one-time event (.once) capturing event (.capture) passive event listening (.passive) Adaptive modifier (.self)Key modifier (.key)

See all articles