Html
<input v-model="keyword" type="text" /> <button @click="onSearch()">搜索</button>
<div>
<span v-if="item in searchList">{{item}}</span>
</div>
vue
export default{
data(){
return{
keyword:"",
searchList:[],
}
},
creaerd(){
this.searchList = JSON.parse(localStorage.getItem('keyword')) || [];
},
methods:{
onSearch(){
const value = this.keyword.trim()
if(!value){
return;
}
if(this.searchList.indexOf(value) === -1){
this.searchList.unshift(value)
this.searchList = this.searchList.slice(0,10)
localStorage.setItem('keyword',JSON.stringify(this.searchList))
}
}
}
}