ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 1.将vuex的代码切割,state,mutations的代码可以放在外部 ### 1.1在外部定义个`state.js`文件 ~~~javascript function getCity() { let defaultCity = "天门"; try { if (localStorage.getItem('city')) { defaultCity = localStorage.getItem('city'); } } catch (e) { console.log(e) } return defaultCity; } export default { city:getCity() } ~~~ ### 1.2`mutations.js` ~~~javascript export default { changeCity(state, city) { //state表示state属性中的数据 state.city = city; try { localStorage.setItem("city", city); } catch (e) { console.log(e); } } } ~~~ ### 1.3在`store.js`中引入就可以了 ~~~javascript import Vue from 'vue' import Vuex from 'vuex' import state from './state' import mutations from './mutations' Vue.use(Vuex) export default new Vuex.Store({ state, mutations }) ~~~