目录

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

 

Logo

基于 Vue 的企业级 UI 组件库和中后台系统解决方案,为数万开发者服务。

更多推荐