Vue实现搜索后地址栏路径改变,刷新界面不改变页面内容功能

1、功能演示

在这里插入图片描述
在这里插入图片描述

2、具体操作

输入搜索值
在这里插入图片描述
点击搜索
在这里插入图片描述
刷新界面之后
在这里插入图片描述

3、实现代码

首先看一下后端接口
在这里插入图片描述

这是我获取列表数据的代码,新增带加号这几行

private getTable(page: number, rows: number): void {
    let params: any = { page, rows };
    + if(this.$route.query.unit) {
    +   params.unit = this.$route.query.unit || '';
    + };
    Object.keys(this.form).forEach((k: string) => (this.form[k] ? (params[k] = this.form[k]) : ''));
    this.setLoading(true);
    deployList(params).then((res: any) => {
      const { code, data } = res;
      if (code === 1) {
        this.pageTotal = data.cnt || 0;
        this.tableData = data.data || [];
        this.tableData.forEach((v: any) => {
          v.commit_id = v.commit_id.slice(0,8);
        });
      };
    });
  }

搜索列表代码

private searchTable(): void {
    this.currenPage = 1;
    this.pageSize = 8;
    if(this.form.unit) {
      if(this.$route.query.unit) {
        if(this.form.unit == this.$route.query.unit) {
          this.getTable(this.currenPage, this.pageSize);
          return;
        }
      }
      this.$router.replace('/pcd/deploy' + '?unit=' + this.form.unit)
    } else {
      if(!this.form.unit && !this.$route.query.unit) {
        this.getTable(this.currenPage, this.pageSize);
        return;
      }
      this.$router.replace('/pcd/deploy');
    }
    this.getTable(this.currenPage, this.pageSize);
  }

4、实现原理

因为两个页面实现原理都是一样的,所以这里直接说想法。通过this.$route中的query来存储unit的值(后端接口要实现搜索功能),在搜索的时候把搜索框内的值赋值到this.$route.query.unit上,刷新界面是,判断this.$route.query.unit是否存在,如果存在直接按照unit的值进行模糊搜索,这里要注意如果路径上也就是this.$route.query.unit的值存在并且你搜索框输入的值与其相同,会导致路径重复报错Navigating to current location,因此这时要在点击搜索的时候进一步判断。

测试的是时候还遇到一些bug,比如搜索的值为空的时候,页面不跳转;搜索值相同的时候,页面显示全部数据;搜索值为空,点击第一次搜索,页面数据不变,再点击一次才出现全部数据等等,可以看一下上面最后改的代码逻辑,本人测现在是没有任何的bug了。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SmallTeddy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值