Просмотр исходного кода

修改虚拟实验室地址栏和返回地址问题

丸子 5 месяцев назад
Родитель
Сommit
cb755986a0

+ 8 - 5
src/views/blockly/Blockly.vue

@@ -231,13 +231,14 @@ const showLampPreview = ref(true);
 const isRecording = ref(false);
 const recordingCountdown = ref(10);
 let countdownInterval = null;
+let recognition = null;
 
 // 返回虚拟实验室
 const goLabShow = () => {
   showLampPreview.value = true;
 };
 const goBack = () => {
-  window.location.href = "/virtual-laboratory";
+  router.push("/virtual-laboratory");
 };
 // 切换灯光状态
 const toggleLight = () => {
@@ -465,7 +466,7 @@ const aiService = {
       }
 
       const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
-      const recognition = new SpeechRecognition();
+      recognition = new SpeechRecognition();
 
       recognition.lang = language;
       recognition.interimResults = false;
@@ -906,9 +907,11 @@ onMounted(() => {
   // 从全局状态初始化年级ID
   state.gradeId = globalState.initGradeId();
 
-  device.value.name = router.currentRoute.value.query.name || "";
-  device.value.image = router.currentRoute.value.query.image || "";
-  device.value.jsonData = JSON.parse(router.currentRoute.value.query.jsonData || {});
+  // 从 window.history.state 接收参数
+  const routeState = window.history.state || {};
+  device.value.name = routeState.name || "";
+  device.value.image = routeState.image || "";
+  device.value.jsonData = routeState.jsonData || {};
 
     // 注册AI语音识别积木
   Blockly.Blocks["ai_voice_input"] = {

+ 8 - 5
src/views/blockly/Blockly2.vue

@@ -248,13 +248,14 @@ const showLampPreview = ref(true);
 const isRecording = ref(false);
 const recordingCountdown = ref(10);
 let countdownInterval = null;
+let recognition = null;
 
 // 返回虚拟实验室
 const goLabShow = () => {
   showLampPreview.value = true;
 };
 const goBack = () => {
-  window.location.href = "/virtual-laboratory";
+  router.push("/virtual-laboratory");
 };
 
 const executeCode = () => {
@@ -487,7 +488,7 @@ const aiService = {
       }
 
       const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
-      const recognition = new SpeechRecognition();
+      recognition = new SpeechRecognition();
 
       recognition.lang = language;
       recognition.interimResults = false;
@@ -1027,9 +1028,11 @@ onMounted(() => {
   // 从全局状态初始化年级ID
   state.gradeId = globalState.initGradeId();
 
-  device.value.name = router.currentRoute.value.query.name || "";
-  device.value.image = router.currentRoute.value.query.image || "";
-  device.value.jsonData = JSON.parse(router.currentRoute.value.query.jsonData || {});
+  // 从 window.history.state 接收参数
+  const routeState = window.history.state || {};
+  device.value.name = routeState.name || "";
+  device.value.image = routeState.image || "";
+  device.value.jsonData = routeState.jsonData || {};
 
   // 注册AI语音识别积木
   Blockly.Blocks["ai_voice_input"] = {

+ 2 - 5
src/views/virtuallaboratory/index.vue

@@ -118,15 +118,12 @@ const fetchLaboratoryList = async () => {
   }
 };
 
-
-
 const handleLabClick = (item) => {
   try {
-    // 立即修改URL,不经过Vue Router的队列处理
-    // window.location.href = item.routePath;
+    // 使用 state 传递参数,地址栏不会显示参数
     router.replace({
       path: item.routePath,
-      query: {
+      state: {
         name: item.name,
         image: item.image,
         jsonData: item.jsonData