一、input输入框 绑定enter键获取keyword值

  1. <div class="search">
  2. <el-input v-model="keyword" @keyup.enter.native="handleUp"> </el-input>
  3. </div>
  4. <div class="name">{{keyword}}</div> // 搜索什么就显示什么

http请求

  1. import axios from 'axios'
  2. export default {
  3. ...
  4. data(){
  5. return{
  6. keyword:"成都",
  7. id:"",
  8. url:"",
  9. }
  10. },
  11. methods:{
  12. async handleUp(){
  13. var url =`http://192.168.14.49:5000/search?keywords=${this.keyword}`;
  14. await axios.get(url).then(res=>{
  15. this.id=res.data.result.songs[0].id; // 获取id
  16. })
  17. var songUrl = `https://music.aityp.com/song/url?id=${this.id}`;
  18. await axios.get(songUrl).then(res=>{
  19. this.url = res.data.data[0].url; // 二次请求http 拿取url
  20. })
  21. }

二、渲染数据

  1. <audio controls autoplay :src="url"></audio>

三、音乐播放,图片旋转

给音乐绑定两个事件

  1. <audio controls autoplay :src="url"
  2. @play="onPlay"
  3. @pause="onPause" ref="audio"></audio>
  1. onPlay(){
  2. this.isPlay=true
  3. },
  4. onPause(){
  5. this.isPlay = false
  6. },

给图片一个点击事件,可以通过控制图片的旋转,暂停来控制音乐的播放和暂停

  1. <img class="pic" @click="handle" src="./assets/20180901160236_mybsx.jpg" alt="">
  1. handle(){
  2. if(this.isPlay){
  3. this.$refs.audio.pause()
  4. this.isPlay = false
  5. }else{
  6. this.$refs.audio.play()
  7. this.isPlay = true
  8. }
  9. }

图片旋转的css样式 ,用三目选算符取控制

  1. <img class="pic" :class="isPlay?'rot':'end'"
  2. @click="handle" src="./assets/20180901160236_mybsx.jpg" alt="">
  1. @keyframes rotated {
  2. 0%{
  3. transform: rotate(0deg)
  4. }
  5. 100%{
  6. transform:rotate(360deg)
  7. }
  8. }
  9. .rot{
  10. animation: rotated 15s linear infinite;
  11. animation-play-state: running;
  12. }
  13. .end{
  14. animation: rotated 15s linear infinite;
  15. animation-play-state: paused;
  16. }