From 91edf31223021eb4cf128d415af3f7ab169edcd6 Mon Sep 17 00:00:00 2001 From: dzq Date: Fri, 18 Apr 2025 17:14:57 +0800 Subject: [PATCH] =?UTF-8?q?refactor(ProductList):=20=E4=BD=BF=E7=94=A8=20`?= =?UTF-8?q?watch`=20=E6=9B=BF=E4=BB=A3=20`onBeforeRouteUpdate`=20=E7=9B=91?= =?UTF-8?q?=E5=90=AC=E8=B7=AF=E7=94=B1=E5=8F=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为了简化代码逻辑,将 `onBeforeRouteUpdate` 替换为 `watch` 监听路由路径变化。这样可以直接在路径变化时触发数据刷新,减少不必要的回调处理。 --- src/pages/product/ProductList.vue | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/pages/product/ProductList.vue b/src/pages/product/ProductList.vue index c97a9f6..b487f8c 100644 --- a/src/pages/product/ProductList.vue +++ b/src/pages/product/ProductList.vue @@ -8,8 +8,10 @@ import VanPopup from "vant/es/popup" import { computed, onBeforeUnmount, onMounted, ref } from "vue" import Cart from "./components/cart.vue" import Detail from "./components/detail.vue" -import { onBeforeRouteUpdate } from 'vue-router' +import { useRoute } from 'vue-router' +const router = useRouter() +const route = useRoute() // 状态管理 const productStore = useProductStore() const cartStore = useCartStore() @@ -115,18 +117,16 @@ onBeforeUnmount(() => { }) // 结算方法 -const router = useRouter() function handleCheckout() { router.push("/product/checkout") } // 路由更新时刷新数据 -onBeforeRouteUpdate((to, from, next) => { - if (to.name == 'ProductList') { +watch(() => route.path, (newPath) => { + if (newPath === '/') { productStore.getGoods() } - next() })