CardIndex.vue 1.6 KB

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