search.vue
2.06 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<template>
<div class="search-container">
<input type="text" placeholder="请输入关键词" v-model="keyword" />
<button
:class="{ 'btn-disabled': !keyword }"
:disabled="!keyword"
@click="goto"
>
搜索
</button>
</div>
</template>
<script>
import { mapGetters, mapMutations } from 'vuex';
export default {
name: "search",
components: {},
props: {
// 是否存储在vuex
isCache: {
type: Boolean,
default: false,
}
},
computed: {
...mapGetters({
cacheKeyword: 'search/keyword'
})
},
data() {
return {
keyword: "",
};
},
watch: {},
created() {
if (!this.isCache) {
this.keyword = this.cacheKeyword;
}
},
mounted() { },
beforeDestroy() {
if (!this.isCache) {
this.setKeyword('');
}
},
methods: {
...mapMutations({
setKeyword: 'search/setKeyword'
}),
goto() {
if (this.isCache) {
this.setKeyword(this.keyword);
}
this.$router.push({
path: `/label/${this.keyword}`,
query: {
search: 'search'
},
});
},
},
};
</script>
<style lang="less" scoped>
.search-container {
background-color: #fff;
margin-top: 20px;
padding: 20px;
border-radius: 4px;
display: flex;
align-items: center;
input {
padding: 0 6px;
border: 1px solid @borderColor;
width: 80%;
height: 26px;
line-height: 26px;
background-color: #f2f2f2;
height: 28px;
font-size: 14px;
border-radius: 2px;
&::placeholder {
color: @assistColor;
}
&:focus {
outline: none;
color: #24292e;
}
}
button {
border-radius: 2px;
background-color: #24292e;
color: #fff;
width: 20%;
border: none;
cursor: pointer;
height: 28px;
line-height: 28px;
margin-left: 12px;
a {
color: #fff;
}
&.btn-disabled {
cursor: not-allowed;
opacity: 0.75;
}
}
}
</style>