vue初学总结

回家以来一直在学习前端

复习html+css,看javascript和vue

对我来说学习新概念,网课效果比只看书要好很多,所以我看完视频再去看书吧

今天用vue+axios写了一个简单的播放器!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>音乐</title>
<style>
* {
padding: 0px;
margin: 0px;
}

#app {
width: 1000px;
margin: 0px auto;
text-align: center;
}

.list {
text-align: left;
}
</style>
</head>

<!-- 不要把vue挂载在body和html上 -->

<body>
<div id="app">
<input type="text" v-model="query" @keyup.enter="searchMusic">
<div style="height: 200px;overflow:scroll;border:1px solid gray;width: 200px;margin:10px auto;">
<ul class="list">
<li v-for="(item,index) in musicList">
<button @click="playMusic(item.id,index)">播放</button>
{{item.name}}
</li>
</ul>
</div>
<div style="margin: 2px;">
<audio ref="audio" :src="musicUrl" controls="controls" autoplay loop></audio>
</div>
<div>
<img :src="albumsrc" alt="" style="width: 128px;height: 128px;">
</div>

</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
var app = new Vue({
el: "#app",
data: {
query: "",
musicList: [],//获取的歌曲列表
musicUrl: "",
albumsrc: "1.jpg",
},
methods: {
//搜索歌曲函数
searchMusic: function () {
var that = this;
axios.get("https://autumnfish.cn/search?keywords=" + this.query)
.then(function (respone) {
// console.log(respone);
that.musicList = respone.data.result.songs;
}, function (error) { })
},
//播放音乐
playMusic: function (musicID, index) {
// console.log(musicID);
var that = this;
axios.get("https://autumnfish.cn/song/url?id=" + musicID).then(function (response) {
// console.log(response);
that.musicUrl = response.data.data[0].url;
});
// axios.get("https://autumnfish.223cn/album?id="+musicID).then(function(response){
// // console.log(response);÷
// })
console.log(that.musicList[index].album.id);
var num = that.musicList[index].album.id;
//判断有没有图片。没有图片就用默认的
if (num != 0) {
axios.get("https://autumnfish.cn/album?id=" + num).then(function (response) {
console.log(response);
that.albumsrc = response.data.album.picUrl;
});
}
else{
that.albumsrc = "1.jpg";
}
},
}
})
</script>
</body>
</html>