Browse Source

优化学习切换

aaa 3 years ago
parent
commit
40e208189a
2 changed files with 32 additions and 38 deletions
  1. 25 20
      src/views/home/learn/page-learn-child.vue
  2. 7 18
      src/views/home/learn/page-learn.vue

+ 25 - 20
src/views/home/learn/page-learn-child.vue

@@ -62,42 +62,47 @@ export default {
   components: {},
   props: {
     // 分组的数据列表
-    learnChildList: {
-      type: Array,
-      default: () => []
+    parentId: {
+      type: String,
+      default: ""
     }
   },
   data() {
     return {
       active: 0,
-      choosedChildIndex: null,
       titleStyle: {
         fontSize: "10px"
       },
+      choosedChildIndex: 0,
       contentList: [],
       loading: false,
-      finished: false
+      finished: false,
+      learnChildList: []
     };
   },
   computed: {},
   created() {},
-  mounted() {},
-  watch: {
-    //   第一次加载内容
-    learnChildList: {
-      handler(newValue, oldValue) {
-        if (this.choosedChildIndex === null) {
-          this.choosedChildIndex = 0;
-        }
-      }
-    },
-    choosedChildIndex(newValue, oldValue) {
-      if (newValue !== oldValue) {
-        this.getContentList();
-      }
-    }
+  mounted() {
+    this.getChildList();
   },
+  watch: {},
   methods: {
+    //   查询子目录
+    getChildList() {
+      let path = {
+        categoryId: this.parentId
+      };
+      this.$_http
+        .get(this.$pathParams(this.$_API.JTXT_GET_CATEGORIES_CATEGROYID, path))
+        .then(res => {
+          res.data.shift();
+          this.learnChildList = res.data;
+          this.getContentList();
+        })
+        .catch(() => {
+          this.$store.commit("toggleLoading", false);
+        });
+    },
     childChange(index) {
       this.finished = false;
       this.choosedChildIndex = index;

+ 7 - 18
src/views/home/learn/page-learn.vue

@@ -7,7 +7,7 @@
         :key="index"
         :title="item.name"
       >
-        <PageLearnChild v-if="index !== 0" :learnChildList="learnChildList">
+        <PageLearnChild v-if="index !== 0" :parentId="parentId">
         </PageLearnChild>
         <PageLearnRecommend v-if="index === 0" :swiperList="swiperList">
         </PageLearnRecommend>
@@ -32,8 +32,8 @@ export default {
       active: 0,
       learnRootList: [],
       choosedRootIndex: 0,
-      learnChildList: [],
-      swiperList: []
+      swiperList: [],
+      parentId: ""
     };
   },
   computed: {},
@@ -61,6 +61,9 @@ export default {
           this.learnRootList = [{ id: "0", name: "推荐", parentId: null }];
           this.learnRootList = [...this.learnRootList, ...res.data];
           //   查询子目录
+          if (this.choosedRootIndex === 0) {
+            return;
+          }
           this.getChildList();
         })
         .catch(() => {
@@ -69,21 +72,7 @@ export default {
     },
     //   查询子目录
     getChildList() {
-      if (this.choosedRootIndex === 0) {
-        return;
-      }
-      let path = {
-        categoryId: this.learnRootList[this.choosedRootIndex].id
-      };
-      this.$_http
-        .get(this.$pathParams(this.$_API.JTXT_GET_CATEGORIES_CATEGROYID, path))
-        .then(res => {
-          res.data.shift();
-          this.learnChildList = res.data;
-        })
-        .catch(() => {
-          this.$store.commit("toggleLoading", false);
-        });
+      this.parentId = this.learnRootList[this.choosedRootIndex].id;
     },
     // 切换根目录
     rootChange(index, title) {