• localStorage
  • sessionStorage

    一、拿取数据,设置缓存

localStorage.setItem(“keyword”,this.keyword)

二、获取缓存

var keyword = localStorage.getItem(“keyword”); // 获取缓存 赋给keyword
if(keyword){
this.keyword = keyword
}

  1. <template>
  2. <div class="home">
  3. <input type="text" v-model="keyword" @keyup.enter="handleEnter">
  4. <!-- 对键盘enter键绑定事件 -->
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name: 'home',
  10. data(){
  11. return{
  12. keyword:""
  13. }
  14. },
  15. mounted(){
  16. var keyword = localStorage.getItem("keyword"); // 获取缓存 赋给keyword
  17. if(keyword){
  18. this.keyword = keyword
  19. }
  20. },
  21. methods:{
  22. handleEnter(){
  23. localStorage.setItem("keyword",this.keyword) //绑定enter键 拿取数据设置缓存
  24. }
  25. }
  26. }
  27. </script>