localStorage.setItem(“keyword”,this.keyword)
二、获取缓存
var keyword = localStorage.getItem(“keyword”); // 获取缓存 赋给keyword
if(keyword){
this.keyword = keyword
}
<template>
<div class="home">
<input type="text" v-model="keyword" @keyup.enter="handleEnter">
<!-- 对键盘enter键绑定事件 -->
</div>
</template>
<script>
export default {
name: 'home',
data(){
return{
keyword:""
}
},
mounted(){
var keyword = localStorage.getItem("keyword"); // 获取缓存 赋给keyword
if(keyword){
this.keyword = keyword
}
},
methods:{
handleEnter(){
localStorage.setItem("keyword",this.keyword) //绑定enter键 拿取数据设置缓存
}
}
}
</script>