localstorage历史搜索记录

mounted: function(){
let vm = this;
if( window. localStorage. getItem( 'SearchKeywords')) {
let searchKeywordListJSON = window. localStorage. getItem( 'SearchKeywords');
vm. serachHistory = JSON. parse( searchKeywordListJSON);
} else {
vm. serachHistory = [];
}
document. onkeyup= function( event) {
var e = event || window. event;
console. log( e. keyCode);
e. preventDefault();
if( e && e. keyCode== 13){ // enter 键
vm. serachHistory. push( vm. searchWord);
if( device. isMeiDeNiApp()) {
ProductsearchBridge. HotSearch( vm. searchWord);
}
else {
window. location. href= '/#/productsearchlist?MainWord='+ vm. searchWord
}
let searchKeywordList = JSON. stringify( vm. serachHistory);
window. localStorage. setItem( 'SearchKeywords', searchKeywordList);
}
};
console. log( window. localStorage);
vm. showHistory();
},
methods:{
showHistory: function() {
let vm = this;
vm. history = [];
let searchKeywordListJSON = window. localStorage. getItem( 'SearchKeywords');
let serachHistory = JSON. parse( searchKeywordListJSON)!= null? JSON. parse( searchKeywordListJSON):[];
for( let i= serachHistory. length- 1; i>= 0; i--) {
vm. history. push( serachHistory[ i]);
}
},
}

你可能感兴趣的:(localstorage历史搜索记录)