index.vue 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <template>
  2. <div class="tab-container">
  3. <el-tag>mounted times :{{ createdTimes }}</el-tag>
  4. <el-alert :closable="false" style="width:200px;display:inline-block;vertical-align: middle;margin-left:30px;" title="Tab with keep-alive" type="success" />
  5. <el-tabs v-model="activeName" style="margin-top:15px;" type="border-card">
  6. <el-tab-pane v-for="item in tabMapOptions" :key="item.key" :label="item.label" :name="item.key">
  7. <keep-alive>
  8. <tab-pane v-if="activeName==item.key" :type="item.key" @create="showCreatedTimes" />
  9. </keep-alive>
  10. </el-tab-pane>
  11. </el-tabs>
  12. </div>
  13. </template>
  14. <script>
  15. import TabPane from './components/TabPane'
  16. export default {
  17. name: 'Tab',
  18. components: { TabPane },
  19. data() {
  20. return {
  21. tabMapOptions: [
  22. { label: 'China', key: 'CN' },
  23. { label: 'USA', key: 'US' },
  24. { label: 'Japan', key: 'JP' },
  25. { label: 'Eurozone', key: 'EU' }
  26. ],
  27. activeName: 'CN',
  28. createdTimes: 0
  29. }
  30. },
  31. watch: {
  32. activeName(val) {
  33. this.$router.push(`${this.$route.path}?tab=${val}`)
  34. }
  35. },
  36. created() {
  37. // init the default selected tab
  38. const tab = this.$route.query.tab
  39. if (tab) {
  40. this.activeName = tab
  41. }
  42. },
  43. methods: {
  44. showCreatedTimes() {
  45. this.createdTimes = this.createdTimes + 1
  46. }
  47. }
  48. }
  49. </script>
  50. <style scoped>
  51. .tab-container {
  52. margin: 30px;
  53. }
  54. </style>