CardIndex.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <template>
  2. <div>
  3. <router-view v-slot="{ Component }">
  4. <keep-alive>
  5. <component :is="Component" />
  6. </keep-alive>
  7. </router-view>
  8. </div>
  9. </template>
  10. <script>
  11. //-----------------------------------------------------------------------------
  12. import vueComponent from '../vueComponent.js';
  13. import _ from 'lodash';
  14. const selfRoute = '/cardindex';
  15. const tab2Route = [
  16. '/cardindex/search',
  17. '/cardindex/card',
  18. '/cardindex/book',
  19. '/cardindex/history',
  20. ];
  21. let lastActiveTab = null;
  22. const componentOptions = {
  23. watch: {
  24. selectedTab: function(newValue) {
  25. lastActiveTab = newValue;
  26. this.setRouteByTab(newValue);
  27. },
  28. curRoute: function(newValue) {
  29. this.setTabByRoute(newValue);
  30. },
  31. },
  32. };
  33. class CardIndex {
  34. _options = componentOptions;
  35. selectedTab = null;
  36. mounted() {
  37. this.setTabByRoute(this.curRoute);
  38. }
  39. setTabByRoute(route) {
  40. const t = _.indexOf(tab2Route, route);
  41. if (t >= 0) {
  42. if (t !== this.selectedTab)
  43. this.selectedTab = t.toString();
  44. } else {
  45. if (route == selfRoute && lastActiveTab !== null)
  46. this.setRouteByTab(lastActiveTab);
  47. }
  48. }
  49. setRouteByTab(tab) {
  50. const t = Number(tab);
  51. if (tab2Route[t] !== this.curRoute) {
  52. this.$router.replace(tab2Route[t]);
  53. }
  54. }
  55. get curRoute() {
  56. const m = this.$route.path.match(/^(\/[^/]*\/[^/]*).*$/i);
  57. return (m ? m[1] : this.$route.path);
  58. }
  59. }
  60. export default vueComponent(CardIndex);
  61. //-----------------------------------------------------------------------------
  62. </script>
  63. <style scoped>
  64. </style>