From 8447aa0b74b808c05f9d84ad920856409e9723b8 Mon Sep 17 00:00:00 2001 From: xixiibn5100 <1584914306@qq.com> Date: Tue, 17 Dec 2024 15:55:25 +0800 Subject: [PATCH 1/2] perf(electricity): sync campus state --- src/pages/electricity/index.vue | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/src/pages/electricity/index.vue b/src/pages/electricity/index.vue index 366841e..84eb73e 100644 --- a/src/pages/electricity/index.vue +++ b/src/pages/electricity/index.vue @@ -96,39 +96,32 @@ import { } from "@/components"; import { useRequest } from "@/hooks"; import { YxyService } from "@/services"; -import { computed, ref, watch } from "vue"; +import { computed, ref } from "vue"; import store, { serviceStore } from "@/store"; import Taro from "@tarojs/taro"; const options = ref(["朝晖/屏峰", "莫干山"]); -const selectedIndex = ref(serviceStore.electricity.selectIndex); +const selectedIndex = computed({ + get: () => serviceStore.electricity.selectIndex, + set: (value) => { + serviceStore.electricity.selectIndex = value; + } +}); + +const selectedOption = computed(() => options.value[selectedIndex.value]); + const valueMap = { "朝晖/屏峰": "zhpf", "莫干山": "mgs" }; -// eslint-disable-next-line no-warning-comments -// TODO: 优化双向同步操作 -// 双向同步 selectedIndex 和 serviceStore.electricity.selectedIndex -watch( - selectedIndex, - (newValue) => { - serviceStore.electricity.selectIndex = newValue; - } -); -watch( - () => serviceStore.electricity.selectIndex, - (newValue) => { - selectedIndex.value = newValue; - } -); - -const selectedOption = computed(() => options.value[selectedIndex.value]); const onPickerChange = (event: { detail: { value: number } }) => { selectedIndex.value = event.detail.value; const selectedValue = valueMap[selectedOption.value]; serviceStore.electricity.electricityCampus = selectedValue; + + // 调用查询接口 getQueryBalance({ campus: selectedValue }); getQueryConsumption({ campus: selectedValue }); }; From fa5f524c1b12fd2237d3d72959291f57a65f8d44 Mon Sep 17 00:00:00 2001 From: xixiibn5100 <1584914306@qq.com> Date: Sat, 4 Jan 2025 15:09:01 +0800 Subject: [PATCH 2/2] perf(electricity): get campus index only by VueX --- src/pages/electricity/index.vue | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/pages/electricity/index.vue b/src/pages/electricity/index.vue index 84eb73e..63456ef 100644 --- a/src/pages/electricity/index.vue +++ b/src/pages/electricity/index.vue @@ -101,12 +101,7 @@ import store, { serviceStore } from "@/store"; import Taro from "@tarojs/taro"; const options = ref(["朝晖/屏峰", "莫干山"]); -const selectedIndex = computed({ - get: () => serviceStore.electricity.selectIndex, - set: (value) => { - serviceStore.electricity.selectIndex = value; - } -}); +const selectedIndex = computed(() => serviceStore.electricity.selectIndex); const selectedOption = computed(() => options.value[selectedIndex.value]); @@ -116,11 +111,10 @@ const valueMap = { }; const onPickerChange = (event: { detail: { value: number } }) => { - selectedIndex.value = event.detail.value; - const selectedValue = valueMap[selectedOption.value]; + const selectedValue = valueMap[options.value[event.detail.value]]; serviceStore.electricity.electricityCampus = selectedValue; - + serviceStore.electricity.selectIndex = event.detail.value; // 调用查询接口 getQueryBalance({ campus: selectedValue }); getQueryConsumption({ campus: selectedValue }); @@ -170,8 +164,7 @@ const { if (response.data.code === 1) { store.commit("setConsumption", response.data.data[0].used); } else if (response.data.code === 200525) { - selectedIndex.value = 0; - serviceStore.electricity.selectedIndex = 0; + serviceStore.electricity.selectIndex = 0; serviceStore.electricity.electricityCampus = "zhpf"; } else { throw new Error(response.data.msg);