前几天有个需求,写一个搜索栏,下面需要附上常用搜索的标签,然后发现Edge主页的样式很好看,于是便实现了一个类似的。
Edge主页:el-card-1.png

自己实现的:el-card-2.png

实现

布局

整体的布局如下:el-card-3.png

实现的时候,父组件:Search.vue,search-bar组件:SearchItem.vue,热点话题组件:SearchHot.vue

实现代码

Search.vue

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
<template>
<div class="box">
<div class="searchBox">
<search-item></search-item>
</div>
<div class="searchBox">
<SearchHot></SearchHot>
</div>
</div>
</template>

<script>
// 引入两个组件,this.$t是使用i18n适配多种语言
import SearchItem from "@/components/SearchItem";
import SearchHot from "@/components/SearchHot";
export default {
name: "Search",
components: {SearchItem,SearchHot},
computed:{
elinputplace(){return this.$t('msg.searchkey')},
},
methods:{
}
}
</script>

<style>
.box{
margin: 5% auto;
padding-top: 6%;
height: 60px;
width: 100%;
}
.searchBox{
margin: 0 auto;
width: 60%;
position: relative;
}
</style>

SearchItem.vue

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
<template>
<div>
<el-input :placeholder=elinputplace v-model="q" class="input-with-select">
<el-button slot="append" type="primary" plain @click="search" icon="el-icon-search">{{$t('msg.search')}}</el-button>
</el-input>
</div>
</template>

<script>
import Hot from "@/components/Hot";
export default {
components:{Hot},
name: "SearchItem",
computed:{
elinputplace(){return this.$t('msg.searchkey')},
eltitle(){return this.$t('msg.notice')}
},
data(){
return{
q:'',
}
},
methods:{
search(){
// 在这里实现点击搜索事件
},
}
}
</script>

<style scoped>
.el-dialog{
width: 60%;
}
</style>

SearchHot.vue

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
<template>
<div>
<el-card class="my-card"
v-for="(item,index) in items"
:key="index"
>
<el-image @click="gethot(item.label)"
:src=item.url
></el-image>
<span class="my-word" @click="gethot(item.label)">{{item.label}}</span>
</el-card>
</div>
</template>

<script>
export default {
name: "SearchHot",
data(){
return{
items: [
{ type: 'info', label: 'Loading',url:require("@/assets/hot/work.png") },
{ type: 'info', label: 'Loading',url:require("@/assets/hot/miss_work.png")},
{ type: 'info', label: 'Loading',url:require("@/assets/hot/course.png")},
{ type: 'info', label: 'Loading',url:require("@/assets/hot/sick.png")},
{ type: 'info', label: 'Loading',url:require("@/assets/hot/study.png")}
],
currentDate: new Date()
}
},
created:function () {
// 获取热点话题并加载到items里面
this.$axios.get('http://xxxxxxxx/hot/').then((response) => {
let data = response.data.slice(1,-1).split(', ')
for(let i = 0;i < 5;i++){
this.items[i].label = data[i].slice(1,-1)
}
}).catch(() => {
});
},
methods:{
gethot(label){
// 点击标签时进行相应的处理
}
}
}
</script>

<style scoped>
.el-card{
width: 120px;
height: 100px;
float: left;
margin-top: 4%;
margin-left: 6%;
background-color: #ecf5ff;
}
.my-card >>> .el-card__body{
padding: 1px;
text-align: center;
}
.my-word{
display: block;
font-size: 13px;
}
.el-image{
text-align: center;
width: 50px;
height: 50px;
margin: 10px;
}

</style>

其他

完整代码和项目可见:完整项目