Explorar el Código

UserHome.vue 中监听路由参数的变化, 便于加载新数据

reghao hace 17 horas
padre
commit
c478a12722
Se han modificado 1 ficheros con 19 adiciones y 0 borrados
  1. 19 0
      src/views/user/UserHome.vue

+ 19 - 0
src/views/user/UserHome.vue

@@ -148,6 +148,17 @@ export default {
     }
   },
   watch: {
+    // 1. 监听路由参数中的 ID 变化 (核心修复)
+    '$route.params.id': {
+      handler(newId) {
+        if (newId) {
+          this.userId = newId;
+          this.resetPageState(); // 切换用户时,重置页码和列表
+          this.initUserContext(); // 重新加载用户资料和关系
+        }
+      },
+      immediate: true // 确保组件创建时也能触发
+    },
     '$route.query': {
       handler() {
         this.loadDataFromRoute();
@@ -160,6 +171,14 @@ export default {
     this.initUserContext();
   },
   methods: {
+    // 新增:切换用户时重置状态,防止数据残留
+    resetPageState() {
+      this.dataList = [];
+      this.currentPage = 1;
+      this.activeName = 'video';
+      this.user = null;
+      this.userContentData = null;
+    },
     initUserContext() {
       Promise.all([
         getUserInfo(this.userId),