element + tabs 页面刷新保持刷新之前的页面

例:
个人中心下的 tab 使用:
实现效果: (1) 当点击“个人中心” 下不同的 tab 跳转到 /userCenter?type=“xx” : 通过 this. r o u t e r . p u s h 实 现 ( 2 ) 当 点 “ 个 人 中 心 ” 的 时 候 , 跳 到 第 一 个 t a b 页 ( 如 果 是 在 第 2 个 t a b 页 , 也 切 换 至 第 一 个 t a b 页 ) : 通 过 监 听 ′ router.push 实现 (2) 当点“个人中心”的时候,跳到第一个 tab 页(如果是在第2个tab页,也切换至第一个tab页) : 通过监听 ' router.push(2)tab2tabtabroute.query’ 实现

一、使用 query

//个人中心下的tab标签使用
<template>
  <div>
    <el-tabs type="card" v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="历史记录" name="1"></el-tab-pane>
      <el-tab-pane label="我的收藏" name="2"></el-tab-pane>
      <el-tab-pane label="我的关注" name="3"></el-tab-pane>
    </el-tabs>
    <div class="tab-content">
      <keep-alive>
        <component :is="curComponents[activeName]"></component>
      </keep-alive>
    </div>
  </div>
</template>

<script>
import History from "./components/history";
import Collect from "./components/collect ";
import Watch from "./components/watch";
export default {
  data() {
    return {
      activeName: '',
      curComponents: {
        '1': 'History',
        '2': 'Collect',
        '3': 'Watch',
      },
    }
  },
  created() {
    this.activeName = this.$route.query.type || '1';
  },
  components: { History, Collect, Watch},
  methods: {
    handleClick(tab) {
      this.activeName = tab.name;
      this.$router.push({
        path: '/userCenter',
        query: { type: this.activeName }
      })
    },
  },
  watch: {
    '$route.query': {
      handler(newval, oldval) {
        this.activeName = newval.type;
      },
      immediate: true
    }
  }
}
</script>

下方是我微信公众号的二维码,可以扫码关注以下,后期博文推送主要在公众号上面,有什么问题也可以通过公众号跟我发消息哦~
在这里插入图片描述

二、使用 params(注:使用 params 的时候,要在配置路由的时候加 /userCenter/:type)

<script>
import History from "./components/history";
import Collect from "./components/collect ";
import Watch from "./components/watch";
export default {
  data() {
    return {
      activeName: '',
      curComponents: {
        '1': 'History',
        '2': 'Collect',
        '3': 'Watch',
      },
    }
  },
  created() {
    this.activeName = this.$route.params.type || '1';
  },
  components: { History, Collect, Watch},
  methods: {
    handleClick(tab) {
      this.activeName = tab.name;
       this.$router.push({
         name: 'userCenter',
         params: { type: this.activeName }
       });
    },
  },
  watch: {
     '$route.params': {
      handler(newval, oldval) {
         // console.log(newval)
         this.activeName = newval.type;
      },
      immediate: true
     },
  }
}
</script>



  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值