vue如何实现清空this.$route.query的值

2022-09-23 09:10:44

目录如何清空this.$route.query的值mounteddestroyed改变this.$route.query中的数据如何清空this.$route.query的值mounted定义一个l...

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

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

mounted

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

//如果有当前localStorage isTest 则 清空query
if(localStorage.gpclHQdUsetItem('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'})
})

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我php们。