场景:地区选择器中,由于方法很长,加载的数据很多,导致页面每进去一次都要加载很久,造成页面卡顿。所以想要把这个数据和方法,提到外面,得到的数据,供所有页面都能正常使用。
使用pinia:
1.main.ts中
import { createPinia } from 'pinia'
2.stores/area.js
import { defineStore } from 'pinia'
import mapData from '../assets/mapData/area.json';
export const useArea = defineStore({
id: 'area',
state: () => {
return {
// navInfo: []
province: [],
cities : [],
counties : []
}
},
getters: {
},
actions: {
getCityData(){
var that = this
// var data = mapData
that.province = []
that.cities = []
that.counties = []
for (var item in mapData) {
if (item.match(/0000$/)) {
//省
this.province.push({ id: item, value: mapData[item], children: [] });
} else if (item.match(/00$/)) {