With the development of Internet technology, music has become an indispensable part of modern people's life. In the music playback interface, Vue.js, as one of the most popular JavaScript frameworks at present, is popular for its concise code and high reproducibility. So, how to import songs when building a music player using Vue.js? Below we will introduce the Vue.js song import method in detail.
First of all, please make sure you have installed Vue.js and imported Vue.js in your project.
First, create a song list in your Vue.js project. Considering the scalability of the program, we do not recommend manually filling in each song in the html file, but using the data binding function of Vue.js to automatically generate a song list.
You can write the following code within the Vue.js component:
<template> <div> <ul> <li v-for="song in songs"> {{song.name}} </li> </ul> </div> </template> <script> export default{ data () { return { songs: [ {'name': '歌曲1', 'src': 'http://xxx.mp3'}, {'name': '歌曲2', 'src': 'http://xxx.mp3'}, {'name': '歌曲3', 'src': 'http://xxx.mp3'} ] } } } </script>
This code will automatically generate a list of three songs in your page. You need to replace the songs
array with your song list.
Now, you have successfully generated a song list and assigned it to the songs
attribute of the Vue.js component . Next, we need to import the song source files into the project.
You can import the song source file in the Vue.js component through the following code:
<template> <div> <ul> <li v-for="(song, index) in songs"> <audio :src="song.url + '/' + song.fileName"></audio> {{song.name}} </li> </ul> </div> </template> <script> export default{ data () { return { songs: [ {'name': '歌曲1', 'url': 'http://xxx.com/songs', 'fileName': 'song1.mp3'}, {'name': '歌曲2', 'url': 'http://xxx.com/songs', 'fileName': 'song2.mp3'}, {'name': '歌曲3', 'url': 'http://xxx.com/songs', 'fileName': 'song3.mp3'} ] } } } </script>
This code updates the song list to a file containing the song name, path and file where the song source file is located. Name object array, and use the <audio>
tag to import each song source file into the page as a component block of Vue.js.
After completing the import of songs, we also need to implement the playback function of the songs.
You can play the song through the following Vue.js code:
<template> <div> <ul> <li v-for="(song, index) in songs" @click="playSong(index)"> {{song.name}} </li> <audio ref="player"></audio> </ul> </div> </template> <script> export default{ data () { return { songs: [ {'name': '歌曲1', 'url': 'http://xxx.com/songs', 'fileName': 'song1.mp3'}, {'name': '歌曲2', 'url': 'http://xxx.com/songs', 'fileName': 'song2.mp3'}, {'name': '歌曲3', 'url': 'http://xxx.com/songs', 'fileName': 'song3.mp3'} ] } }, methods: { playSong (index) { let player = this.$refs.player player.src = this.songs[index].url + '/' + this.songs[index].fileName player.play() } } } </script>
This code contains a playSong(index)
method, which is used when the user clicks Triggered when the song list is triggered, add the path of the specified song source file to the src
attribute of the <audio>
tag of Vue.js, and call play()
Method to play songs.
Now, you have successfully built a simple music player using Vue.js and successfully imported songs.
Summary:
Vue.js is an extremely excellent javascript framework, which brings us a lot of convenience. In this article, we introduce the method of using Vue.js to import songs and realize the playback of songs. If you are not yet familiar with Vue.js, I hope this article will be helpful to you.
The above is the detailed content of How to import songs into vue music. For more information, please follow other related articles on the PHP Chinese website!