ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
~~~ ## 1.使用缓冲实现数据的持久化 ~~~javascript localStorage.setItem("city",citye); localStorage.getItem("city") //技术要点,使用localStorage的时候使用try-catch对代码进行包裹 try{ localStorage.setItem("city",city) }catch(e){ console.log(e) } ~~~ - 在`store.js`中设置 ~~~javascript import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ state: { city:getCity() }, mutations: { changeCity(state,city){ //state表示state属性中的数据 state.city = city; localStorage.setItem("city",city); } }, }) function getCity(){ let defaultCity = "天门"; try{ if(localStorage.getItem('city')){ defaultCity = localStorage.getItem('city'); } }catch(e){ console.log(e) } return defaultCity; } ~~~ ~~~