index.vue 1.4 KB
Newer Older
P
Pan 已提交
1 2
<template>
  <div class="tab-container">
3
    <el-tag>mounted times :{{ createdTimes }}</el-tag>
J
Jere 已提交
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">
J
Jere 已提交
6
      <el-tab-pane v-for="item in tabMapOptions" :key="item.key" :label="item.label" :name="item.key">
P
Pan 已提交
7
        <keep-alive>
J
Jere 已提交
8
          <tab-pane v-if="activeName==item.key" :type="item.key" @create="showCreatedTimes" />
P
Pan 已提交
9 10 11 12 13 14 15
        </keep-alive>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
16
import tabPane from './components/TabPane'
P
Pan 已提交
17

P
Pan 已提交
18
export default {
19
  name: 'Tab',
P
Pan 已提交
20 21 22 23
  components: { tabPane },
  data() {
    return {
      tabMapOptions: [
花裤衩 已提交
24 25 26 27
        { label: 'China', key: 'CN' },
        { label: 'USA', key: 'US' },
        { label: 'Japan', key: 'JP' },
        { label: 'Eurozone', key: 'EU' }
P
Pan 已提交
28 29 30 31 32
      ],
      activeName: 'CN',
      createdTimes: 0
    }
  },
花裤衩 已提交
33 34 35 36 37 38
  watch: {
    activeName(val) {
      this.$router.push(`${this.$route.path}?tab=${val}`)
    }
  },
  created() {
花裤衩 已提交
39
    // init the default selected tab
花裤衩 已提交
40 41 42 43 44
    const tab = this.$route.query.tab
    if (tab) {
      this.activeName = tab
    }
  },
P
Pan 已提交
45 46 47
  methods: {
    showCreatedTimes() {
      this.createdTimes = this.createdTimes + 1
P
Pan 已提交
48 49
    }
  }
P
Pan 已提交
50
}
P
Pan 已提交
51 52 53
</script>

<style scoped>
54
  .tab-container {
P
Pan 已提交
55 56 57
    margin: 30px;
  }
</style>