$router、$route、$router.push()和、$router.replace()

目录

1.this.$router

2.this.$route

3.console.log(this.$router)和console.log(this.$route)打印信息

4.push和replace区别

5.push 传递参数和接收参数


1.this.$router

是VueRouter的一个对象,表示全局路由器对象,项目中通过router路由参数注入路由之后,在任何一个页面都可以通过此方法获取到路由器对象,并调用其push(), go()等方法

2.this.$route

表示当前正在用于跳转的路由器对象,可以调用其name、path、query、params等方法

3.console.log(this.$router)和console.log(this.$route)打印信息

 4.push和replace区别

$router.push({path:'home'});本质是向history栈中添加一个路由,在我们看来是切换路由,但本质是在添加一个history记录

$router.replace({path:'home'});替换路由,没有历史记录,点击返回,会跳转到上上一个页面

5.push 传递参数和接收参数

字符串
this.$router.push(name)
this.$router.push(path)

对象
传参:name搭配params (类似post)
this.$router.push({
        name:'xxx',
        params:{
          id:id
        }
      })
  
接收参数:
this.$route.params.id

传参: path搭配query (类似get)
this.$router.push({
        path:'/xxx',
        query:{
          id:id
        }
      })
  
接收参数:
this.$route.query.id

 

  • 17
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值