vue搜索页开发实例代码详解(热门搜索,历史搜索,淘宝接口演

2020-06-16 06:58:52易采站长站整理

历史搜索组件

src/pages/search/history.vue


<template>
<div class="history" v-if="historys.length">
<h4 class="history-title">历史搜索</h4>
<transition-group class="g-list" tag="ul" name="list">
<li class="g-list-item" v-for="item in historys" :key="item" @click="$_selectItem(item)">
<span class="g-list-text">{{item}}</span>
<!-- .stop 禁止事件冒泡 -->
<i class="iconfont icon-delete" @click.stop="removeItem(item)"></i>
</li>
</transition-group>
<a href="javascript:;" rel="external nofollow" class="history-btn" @click="showConfirm">
<i class="iconfont icon-clear" ></i>
清空历史搜索
</a>
</div>
</template>

<script>
import storage from 'assets/js/storage';
import {SEARCH_HISTORY_KEYWORD_KEY} from 'pages/search/config';
import {searchMixin} from 'api/mixins';

export default {
name:'SearchHistory',
data(){
return{
historys:[] }
},
mixins:[searchMixin],
created(){
this.getKeyword();
},
methods:{
update(){
this.getKeyword();
},
getKeyword(){
this.historys=storage.get(SEARCH_HISTORY_KEYWORD_KEY,[]);
this.$emit('loaded');
},
removeItem(item){
this.historys=this.historys.filter(val=>val!==item);//点击后删除该项
storage.set(SEARCH_HISTORY_KEYWORD_KEY,this.historys);//更新缓存
this.$emit('remove-item');
},
showConfirm(){
this.$emit('show-confirm');
},
clear(){
storage.remove(SEARCH_HISTORY_KEYWORD_KEY);
}
}
}
</script>

<style lang="scss" scoped>

$border-color: #e5e5e5;
$font-size-base: 12px;
$font-size-l: $font-size-base + 2;
$border-color: #e5e5e5;

@mixin flex-center($direction: row) {
display: flex;
justify-content: center;
align-items: center;
flex-direction: $direction;
}

.history {
padding-bottom: 30px;
background-color: #fff;

&-title {
height: 34px;
line-height: 34px;
padding: 0 10px;
font-size: $font-size-l;
font-weight: bold;
}

&-btn {
@include flex-center();
width: 80%;
height: 40px;
background: none;
border: 1px solid #ccc;
border-radius: 4px;
margin: 0 auto;
color: #686868;

.iconfont {
margin-right: 5px;
}
}
}

.g-list {
border-top: 1px solid $border-color;
border-bottom: 1px solid $border-color;
margin-bottom: 20px;