Преглед изворни кода

feat(news-detail): 优化页面参数获取逻辑,使用 onLoad 钩子和 ref 处理参数

mcbaiyun пре 2 недеља
родитељ
комит
476c3b9523
1 измењених фајлова са 8 додато и 2 уклоњено
  1. 8 2
      src/pages/public/news-detail.vue

+ 8 - 2
src/pages/public/news-detail.vue

@@ -51,10 +51,17 @@
 
 <script setup lang="ts">
 import { ref, computed, onMounted, watch, onUnmounted } from 'vue'
+import { onLoad } from '@dcloudio/uni-app'
 import { downloadWithAuth } from '@/utils/downloadWithAuth'
 import CustomNav from '@/components/custom-nav.vue'
 import { getNewsById } from '@/api/news'
 
+const query = ref<Record<string, string>>({})
+
+onLoad((options) => {
+  query.value = options || {}
+})
+
 // 处理内联markdown
 const processInline = (text: string) => {
   return text.replace(/\*\*(.*?)\*\*/g, '$1').replace(/\*(.*?)\*/g, '$1');
@@ -356,8 +363,7 @@ const onImageError = async (src?: string, e?: any) => {
 }
 
 onMounted(async () => {
-  const query: Record<string, string> = (((globalThis as any).getCurrentPages ? (globalThis as any).getCurrentPages().slice(-1)[0]?.options : {}) as Record<string, string>) || {}
-  const id = query.id || ''
+  const id = query.value.id || ''
   if (!id) {
     isLoading.value = false
     return