Skip to content

Commit 65a7b54

Browse files
recocopliotzsw
authored andcommitted
refactor: Package TagList component
1 parent ad29e9f commit 65a7b54

File tree

3 files changed

+72
-82
lines changed

3 files changed

+72
-82
lines changed

components/HomeBlog.vue

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,7 @@
4848
</ul>
4949
<hr>
5050
<h4><i class="iconfont reco-tag"></i> 标签</h4>
51-
<div class="tags">
52-
<span
53-
v-for="(item, index) in tags"
54-
:key="index"
55-
:style="{ 'backgroundColor': item.color }"
56-
@click="getPagesByTags(item.name)">{{item.name}}</span>
57-
</div>
51+
<TagList @getCurrentTag="getPagesByTags"></TagList>
5852
</div>
5953
</div>
6054

@@ -87,12 +81,11 @@
8781

8882
<script>
8983
import AccessNumber from '@theme/components/Valine/AccessNumber'
84+
import TagList from '@theme/components/TagList.vue'
9085
import NoteAbstract from '@theme/components/NoteAbstract.vue'
91-
import mixin from '@theme/mixins/index.js'
9286
9387
export default {
94-
mixins: [mixin],
95-
components: { AccessNumber, NoteAbstract },
88+
components: { AccessNumber, NoteAbstract, TagList },
9689
data () {
9790
return {
9891
recoShow: false,
@@ -156,17 +149,6 @@ export default {
156149
return document.querySelector('.hero').clientHeight
157150
}
158151
},
159-
created () {
160-
if (this.$tags.list.length > 0) {
161-
const tags = this.$tags.list
162-
tags.map(item => {
163-
const color = this._tagColor()
164-
item.color = color
165-
return tags
166-
})
167-
this.tags = tags
168-
}
169-
},
170152
mounted () {
171153
this.recoShow = true
172154
},
@@ -318,28 +300,6 @@ export default {
318300
}
319301
}
320302
}
321-
.tags {
322-
margin-bottom 30px
323-
span {
324-
vertical-align: middle;
325-
margin: 4px 4px 10px;
326-
padding: 4px 8px;
327-
display: inline-flex;
328-
cursor: pointer;
329-
border-radius: 2px;
330-
background: #fff;
331-
color: #fff;
332-
font-size: 13px;
333-
box-shadow 0 1px 4px 0 rgba(0,0,0,0.2)
334-
transition: all .5s
335-
&:hover {
336-
transform scale(1.04)
337-
}
338-
&.active {
339-
transform scale(1.2)
340-
}
341-
}
342-
}
343303
}
344304
}
345305

components/TagList.vue

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<template>
2+
<div class="tags">
3+
<span
4+
v-for="(item, index) in tags"
5+
:key="index"
6+
:class="{'active': item.name == currentTag}"
7+
:style="{ 'backgroundColor': item.color }"
8+
@click="tagClick(item.name)">{{item.name}}</span>
9+
</div>
10+
</template>
11+
12+
<script>
13+
import mixin from '@theme/mixins/index.js'
14+
15+
export default {
16+
mixins: [mixin],
17+
props: {
18+
currentTag: {
19+
type: String,
20+
default: ''
21+
}
22+
},
23+
data () {
24+
return {
25+
tags: []
26+
}
27+
},
28+
created () {
29+
if (this.$tags.list.length > 0) {
30+
const tags = this.$tags.list
31+
tags.map(item => {
32+
const color = this._tagColor()
33+
item.color = color
34+
return tags
35+
})
36+
this.tags = [{ name: '全部', color: this._tagColor() }, ...tags]
37+
}
38+
},
39+
methods: {
40+
tagClick (tag) {
41+
this.$emit('getCurrentTag', tag)
42+
}
43+
}
44+
}
45+
</script>
46+
47+
<style lang="stylus" scoped>
48+
.tags
49+
margin 30px 0
50+
span
51+
vertical-align: middle;
52+
margin: 4px 4px 10px;
53+
padding: 4px 8px;
54+
display: inline-block;
55+
cursor: pointer;
56+
border-radius: 2px;
57+
background: #fff;
58+
color: #fff;
59+
font-size: 13px;
60+
box-shadow 0 1px 4px 0 rgba(0,0,0,0.2)
61+
transition: all .5s
62+
&:hover
63+
transform scale(1.04)
64+
&.active
65+
transform scale(1.2)
66+
</style>

layouts/Tags.vue

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
<template>
22
<div class="tags-wrapper" :class="recoShow?'reco-show': 'reco-hide'">
33
<Common :sidebar="false" :isComment="false"></Common>
4-
<div class="tags">
5-
<span
6-
v-for="(item, index) in tags"
7-
:key="index"
8-
:class="{'active': item.name == currentTag}"
9-
:style="{ 'backgroundColor': item.color }"
10-
@click="tagClick(item.name)">{{item.name}}</span>
11-
</div>
4+
<TagList :currentTag="currentTag" @getCurrentTag="tagClick"></TagList>
125
<note-abstract
136
class="list"
147
:data="posts"
@@ -26,13 +19,11 @@
2619

2720
<script>
2821
import Common from '@theme/components/Common.vue'
22+
import TagList from '@theme/components/TagList.vue'
2923
import NoteAbstract from '@theme/components/NoteAbstract.vue'
30-
import mixin from '@theme/mixins/index.js'
3124
3225
export default {
33-
mixins: [mixin],
34-
components: { Common, NoteAbstract },
35-
26+
components: { Common, NoteAbstract, TagList },
3627
data () {
3728
return {
3829
posts: [],
@@ -43,7 +34,6 @@ export default {
4334
allTagName: '全部'
4435
}
4536
},
46-
4737
computed: {
4838
// 时间降序后的博客列表
4939
handlePosts () {
@@ -58,17 +48,9 @@ export default {
5848
return posts
5949
}
6050
},
61-
6251
created () {
6352
if (this.$tags.list.length > 0) {
6453
const currentTag = this.$route.query.tag ? this.$route.query.tag : this.currentTag
65-
const tags = this.$tags.list
66-
tags.map(item => {
67-
const color = this._tagColor()
68-
item.color = color
69-
return tags
70-
})
71-
this.tags = [{ name: '全部', color: this._tagColor() }, ...tags]
7254
7355
this.getPagesByTags(currentTag)
7456
}
@@ -135,24 +117,6 @@ export default {
135117
max-width: 740px;
136118
margin: 0 auto;
137119
padding: 4.6rem 2.5rem 0;
138-
.tags
139-
margin 30px 0
140-
span
141-
vertical-align: middle;
142-
margin: 4px 4px 10px;
143-
padding: 4px 8px;
144-
display: inline-block;
145-
cursor: pointer;
146-
border-radius: 2px;
147-
background: #fff;
148-
color: #fff;
149-
font-size: 13px;
150-
box-shadow 0 1px 4px 0 rgba(0,0,0,0.2)
151-
transition: all .5s
152-
&:hover
153-
transform scale(1.04)
154-
&.active
155-
transform scale(1.2)
156120
&.reco-hide {
157121
.tags, .list, .pagation {
158122
load-start()

0 commit comments

Comments
 (0)