如何通过Vue清空路由查询参数this.$route.query的值?

2026-04-02 07:071阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计356个文字,预计阅读时间需要2分钟。

如何通过Vue清空路由查询参数this.$route.query的值?

目录- 如何清除this.$route.query的值- mounted和destroyed生命周期- 如何修改this.$route.query中的数据- 如何清除this.$route.query的值- 如何通过localStorage判断是否是携带参数进来的页面- 如果有当前

目录
  • 如何清空this.$route.query的值
    • mounted
    • destroyed
  • 改变this.$route.query中的数据

    如何清空this.$route.query的值

    mounted

    定义一个 localStorage 来判断当前是否是query携带参数进来页面的

    如何通过Vue清空路由查询参数this.$route.query的值?

    //如果有当前localStorage isTest 则 清空query  if(localStorage.getItem('isTest')){      this.$router.push({ query: {} })  } //  if(this.$route && this.$route.query && this.$route.query.id){      //定义一个 localStorage isTest      localStorage.setItem('isTest', true)      // 这里处理携带进来的参数  }

    destroyed

    destroyed() {    localStorage.removeItem('isTest') }

    改变this.$route.query中的数据

    某些特定情况下想改变this.$route.query中的数据(就是地址栏中的参数),是不能通过直接赋值来改变的,我目前知道的一个办法是下载webpack-merge依赖包实现:

    下载:

    npm i webpack-merge

    引入:

    import merge from 'webpack-merge';

    修改:

    this.$router.push({     query:merge(this.$route.query,{'id':'630'}) })

    如果id本来存在的话就是修改id的值,不存在的话就会添加id这个属性,如果想替换所有的属性只要把’id’换成{}就可以。

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持易盾网络。

    本文共计356个文字,预计阅读时间需要2分钟。

    如何通过Vue清空路由查询参数this.$route.query的值?

    目录- 如何清除this.$route.query的值- mounted和destroyed生命周期- 如何修改this.$route.query中的数据- 如何清除this.$route.query的值- 如何通过localStorage判断是否是携带参数进来的页面- 如果有当前

    目录
    • 如何清空this.$route.query的值
      • mounted
      • destroyed
    • 改变this.$route.query中的数据

      如何清空this.$route.query的值

      mounted

      定义一个 localStorage 来判断当前是否是query携带参数进来页面的

      如何通过Vue清空路由查询参数this.$route.query的值?

      //如果有当前localStorage isTest 则 清空query  if(localStorage.getItem('isTest')){      this.$router.push({ query: {} })  } //  if(this.$route && this.$route.query && this.$route.query.id){      //定义一个 localStorage isTest      localStorage.setItem('isTest', true)      // 这里处理携带进来的参数  }

      destroyed

      destroyed() {    localStorage.removeItem('isTest') }

      改变this.$route.query中的数据

      某些特定情况下想改变this.$route.query中的数据(就是地址栏中的参数),是不能通过直接赋值来改变的,我目前知道的一个办法是下载webpack-merge依赖包实现:

      下载:

      npm i webpack-merge

      引入:

      import merge from 'webpack-merge';

      修改:

      this.$router.push({     query:merge(this.$route.query,{'id':'630'}) })

      如果id本来存在的话就是修改id的值,不存在的话就会添加id这个属性,如果想替换所有的属性只要把’id’换成{}就可以。

      以上为个人经验,希望能给大家一个参考,也希望大家多多支持易盾网络。