Vue.js页面跳转后返回上一页面记录上一页面select选定的值

在当前页面中,选中了某个标签中的问题,然后跳转到问题页面,按返回按钮返回到当前页面,继续选中之前的选中标签
实现效果如下图:
在这里插入图片描述
实现逻辑如下:

  • 进入当前页面的时候,页面路由为http://localhost:8080/#/problem
  • 点击某个标签,页面路径修改为http://localhost:8080/#/problem?index=3
  • index的值为选中的标签下标,这样就用当前路由记录了当前的标签选中项
  • 进入详情页后,路由为http://localhost:8080/#/detail/?id=57
  • 返回上级页面,页面路由为http://localhost:8080/#/problem?index=3
  • 在页面渲染的时候,获取路由的参数,获取到index的值为3
  • 然后就将标签的选中项,设置为下标为3的标签,
  • 同时请求下标为3的标签项对应的问题内容,填充到右侧

代码如下:
给页面路由添加index内容

 //在点击标签的时候调用
 this.$router.replace({
    
    
        query: {
    
    
          ...this.$route.query,
          index: index
        }
      })
// 判断路由中是否有index
 if (this.$route.query.index) {
    
    
 	    // 走选中之前页面选中项的逻辑
        let index = this.$route.query.index
       	// 设置标签的默认选中项
        this.activeKey = index
        // 获取选中项的右侧问题内容
        let typeId = this.questionHeadData[index].typeId
        this.oneId = typeId
        this.getQuestionMore(typeId)
      } else {
    
    
      // 走正常的页面渲染逻辑
        const typeId = this.questionHeadData[0].typeId
        this.oneId = typeId
       	// 请求右侧数据
        this.getQuestionMore(typeId)
      }

以上就是整个实现默认选中之前选中项的逻辑,如果疑问,欢迎评论区留言。

猜你喜欢

转载自blog.csdn.net/weixin_42321819/article/details/109747609