Browse Source

Небольшие переделки маршрутизации, рефакторинг

Book Pauk 6 years ago
parent
commit
8b30409a7d
3 changed files with 25 additions and 16 deletions
  1. 3 1
      client/components/App.vue
  2. 15 8
      client/components/CardIndex/CardIndex.vue
  3. 7 7
      client/router.js

+ 3 - 1
client/components/App.vue

@@ -36,7 +36,9 @@
         </el-aside>
         </el-aside>
 
 
         <el-main>
         <el-main>
-            <router-view></router-view>
+            <keep-alive>
+                <router-view></router-view>
+            </keep-alive>
         </el-main>
         </el-main>
     </el-container>
     </el-container>
 </template>
 </template>

+ 15 - 8
client/components/CardIndex/CardIndex.vue

@@ -18,6 +18,7 @@ import Vue from 'vue';
 import Component from 'vue-class-component';
 import Component from 'vue-class-component';
 import _ from 'lodash';
 import _ from 'lodash';
 
 
+const rootRoute = '/cardindex';
 const tab2Route = [
 const tab2Route = [
     '/cardindex/search',
     '/cardindex/search',
     '/cardindex/card',
     '/cardindex/card',
@@ -30,10 +31,7 @@ export default @Component({
     watch: {
     watch: {
         selectedTab: function(newValue, oldValue) {
         selectedTab: function(newValue, oldValue) {
             lastActiveTab = newValue;
             lastActiveTab = newValue;
-            const t = Number(newValue);
-            if (tab2Route[t] !== this.curRoute) {
-                this.$router.replace(tab2Route[t]);
-            }
+            this.setRouteByTab(newValue);
         },
         },
         curRoute: function(newValue, oldValue) {
         curRoute: function(newValue, oldValue) {
             this.setTabByRoute(newValue);
             this.setTabByRoute(newValue);
@@ -45,14 +43,23 @@ class CardIndex extends Vue {
 
 
     mounted() {
     mounted() {
         this.setTabByRoute(this.curRoute);
         this.setTabByRoute(this.curRoute);
-        if (lastActiveTab !== null)
-            this.selectedTab = lastActiveTab;
     }
     }
 
 
     setTabByRoute(route) {
     setTabByRoute(route) {
         const t = _.indexOf(tab2Route, route);
         const t = _.indexOf(tab2Route, route);
-        if (t >= 0 && t !== this.selectedTab) {
-            this.selectedTab = t.toString();
+        if (t >= 0) {
+            if (t !== this.selectedTab)
+                this.selectedTab = t.toString();
+        } else {
+            if (route == rootRoute && lastActiveTab !== null)
+                this.setRouteByTab(lastActiveTab);
+        }
+    }
+
+    setRouteByTab(tab) {
+        const t = Number(tab);
+        if (tab2Route[t] !== this.curRoute) {
+            this.$router.replace(tab2Route[t]);
         }
         }
     }
     }
 
 

+ 7 - 7
client/router.js

@@ -4,13 +4,13 @@ import _ from 'lodash';
 
 
 import App from './components/App.vue';
 import App from './components/App.vue';
 
 
-import CardIndex from './components/CardIndex/CardIndex.vue';
-import Search from  './components/CardIndex/Search/Search.vue';
-import Card from  './components/CardIndex/Card/Card.vue';
-import Book from  './components/CardIndex/Book/Book.vue';
-import History from  './components/CardIndex/History/History.vue';
+const CardIndex = () => import('./components/CardIndex/CardIndex.vue');
+const Search = () => import('./components/CardIndex/Search/Search.vue');
+const Card = () => import('./components/CardIndex/Card/Card.vue');
+const Book = () => import('./components/CardIndex/Book/Book.vue');
+const History = () => import('./components/CardIndex/History/History.vue');
 
 
-import Reader from './components/Reader/Reader.vue';
+const Reader = () => import('./components/Reader/Reader.vue');
 //const Forum = () => import('./components/Forum/Forum.vue');
 //const Forum = () => import('./components/Forum/Forum.vue');
 const Income = () => import('./components/Income/Income.vue');
 const Income = () => import('./components/Income/Income.vue');
 const Sources = () => import('./components/Sources/Sources.vue');
 const Sources = () => import('./components/Sources/Sources.vue');
@@ -20,7 +20,7 @@ const NotFound404 = () => import('./components/NotFound404/NotFound404.vue');
 
 
 const myRoutes = [
 const myRoutes = [
     ['/', null, null, '/cardindex'],
     ['/', null, null, '/cardindex'],
-    ['/cardindex', CardIndex, null, '/cardindex/search' ],
+    ['/cardindex', CardIndex ],
     ['/cardindex~search', Search ],
     ['/cardindex~search', Search ],
     ['/cardindex~card', Card ],
     ['/cardindex~card', Card ],
     ['/cardindex~card/:authorId', Card ],
     ['/cardindex~card/:authorId', Card ],