Vue用router.push(传参)跳转页面,参数改变,跳转页面数据不刷新的解决办法

本文介绍了Vue里this.$router和this.$route的区别,this.$router是全局路由器对象,可调用push等方法;this.$route是当前路由对象,能获取相关属性。还讲解了push方法的使用、传参规则,以及params传参的注意事项,最后提及参数改变页面数据不刷新的解决办法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

this.$router 相当于一个全局的路由器对象,包含了很多属性和对象(比如 history 对象),任何页面都可以调用其 push(), replace(), go() 等方法。

this.$route 表示当前路由对象,每一个路由都会有一个 route 对象,是一个局部的对象,可以获取对应的 name, path, params, query 等属性。

 

关于 push() 方法:

想要导航到不同的 URL,则使用 router.push 方法。这个方法会向 history 栈添加一个新的记录,所以,当用户点击浏览器后退按钮时,则回到之前的 URL。

当你点击 <router-link> 时,这个方法会在内部调用,所以说,点击 <router-link :to="..."> 等同于调用 router.push(...)。

push() 方法的调用:
 

    //字符串
    this.$router.push('home')
 
    //对象
    this.$router.push({path:'home'})
 
    //命名的路由
    this.$router.push({name:'user', params:{userId: '123'}})
 
    //带查询参数,变成 /register?plan=private
    this.$router.push({path:'register', query:{plan:private}})

  注意:如果提供了 path,params 会被忽略,上述例子中的 query 并不属于这种情况。取而代之的是下面例子的做法,你需要提供路由的 name 或手写完整的带有参数的 path:

    const userId = '123';
 
    this.$router.push({path:`/user/${userId}`});  //->/user/123
 
    this.$router.push({name:'user', params:{userId}});  //->/user/123
 
    //这里的 params 不生效
    this.$router.push({path:'/user', params:{userId}});  //->/user

总结:

params 传参,push 里面只能是 name:'xxx',不能是 path:'/xxx',因为 params 只能用 name 来引入路由,如果这里写成了 path ,接收参数页面会是 undefined。

1、手写完整的 path:
 
    this.$router.push({path: `/user/${userId}`});
 
    获取参数:this.$route.params.userId
 
2、用 params 传递:
 
    this.$router.push({name:'user', params:{userId: '123'}});
 
    获取参数:this.$route.params.userId
 
    url 形式:url 不带参数,http:localhost:8080/#/user
 
3、用 query 传递:
 
    this.$router.push({path:'/user', query:{userId: '123'}});
 
    获取参数:this.$route.query.userId
 
    url 形式:url 带参数,http:localhost:8080/#/user?userId=123

Vue用router.push(传参)跳转页面,参数改变,跳转页面数据不刷新的解决办法:

// go方式   再跳转后的路由观察路由变化,进行页面刷新。 全局作用

watch: {    

    '$route' (to, from) {   

         this.$router.go(0);   

 }}


//  局部处理方式
watch: {
	  '$route' (to, from) { //监听路由是否变化
		  if(to.query.id != from.query.id){
			  this.id = to.query.id;
			  this.init();//重新加载数据
		  }
	  }
},
created() {
	if(this.$route.query) {
		this.id = this.$route.query.id;
		this.init();
	}
}

 

Vue 3 中使用 `router.push` 进行路由跳转并传递参数时,可以通过 `params` 或 `query` 实现。这两种方式分别适用于同的场景:`params` 用于隐藏参数传参,而 `query` 则会将参数显示在 URL 地址栏中 [^3]。 ### 使用 `params` 传参 若需要通过 `params` 传递参数,则必须使用命名路由(`name`)而是路径(`path`)。例如: ```javascript this.$router.push({ name: 'post', params: { id: 123 } }); ``` 在目标组件中,可以通过 `this.$route.params.id` 获取该参数值。这种方式类似于 POST 请求的行为,适用于希望将参数暴露在地址栏中的情况 [^5]。 需要注意的是,在动态路由中使用 `params` 传参时,如果直接使用 `path` 而是 `name`,会导致 `params` 参数无效,并且目标页面接收到的参数为 `undefined` [^4]。 ### 使用 `query` 传参 如果希望通过 URL 显示传递的参数,则可以使用 `query` 方式。此时既可以使用 `name` 也可以使用 `path` 来指定目标路由: ```javascript this.$router.push({ path: '/post', query: { id: 123 } }); ``` 或者使用 `name` 指定路由名称: ```javascript this.$router.push({ name: 'post', query: { id: 123 } }); ``` 在目标组件中,可以通过 `this.$route.query.id` 获取参数值。这种方式类似于 GET 请求的行为,适合用于分享链接或需要记录参数历史的场景 [^3]。 ### 动态路由与 `params` 的结合使用 Vue Router 支持定义带有参数的动态路由。例如,以下代码定义了一个包含 `author` 参数的路径: ```javascript const router = new VueRouter({ routes: [ { path: '/post/:author', component: Author } ] }); ``` 在模板中可以通过 `$route.params.author` 直接访问该参数值: ```vue <template> <div>Author: {{ $route.params.author }}</div> </template> ``` 这种方式使得 URL 中的路径段可以作为数据传递的一部分使用,同时保持路由结构的灵活性 [^2]。 ### 注意事项 - `params` 必须配合 `name` 使用,能与 `path` 同时使用;否则 `params` 将失效 [^4]。 - `query` 可以通过 `path` 或 `name` 指定路由,但敏感信息可能会被暴露在 URL 中 [^3]。 - 在刷新页面后,`params` 传递的参数会保留在 URL 中,因此可能导致数据丢失;而 `query` 由于显示在 URL 中,因此会丢失 [^3]。 ### 示例代码 #### 发起跳转(使用 `name + params`): ```javascript this.$router.push({ name: 'anotherPage', params: { id: 1 } }); ``` #### 接收参数(在目标页面中): ```javascript console.log(this.$route.params.id); // 输出: 1 ``` #### 发起跳转(使用 `path + query`): ```javascript this.$router.push({ path: '/anotherPage', query: { id: 1 } }); ``` #### 接收参数(在目标页面中): ```javascript console.log(this.$route.query.id); // 输出: 1 ``` 综上所述,选择 `params` 还是 `query` 取决于具体需求:若需要隐藏参数,则使用 `params`;若需要参数可见或用于分享链接,则使用 `query` [^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值