编程式导航:基本跳转(如何实现点击按钮跳转)

发布时间 2023-10-11 17:18:49作者: 嘎嘎鸭2

问题:如何实现点击按钮跳转?

答:有两种跳转方式。

 

方法一: path 路径跳转(简易方便)

eg:

按钮的点击事件中写:

1. 不传参:this.$router.push ( ' /路由路径 ' )  (比如:this.$router.push ( ' /search ' ) )

2. 传参:

 ① this.$router.push (' /路径?参数名1=参数值1 & 参数名2=参数值2 ')   → 如果其他页面要接收传过来的参数,可以用 $route.query.参数名

 ② this.$router.push ( ' /路径/参数值 ' ) → 如果其他页面要接收传过来的参数,可以用 $route.params.参数名

或者

1. 不传参:

this.$router.push ({

      path : ' /路径 '

})

2. 传参:

①($route.query.参数名  来接收传过来的参数)

this.$router.push ({

      path : ' /路径 ' ,

      query : {

            参数名1 : ' 参数值1 ' ,

            参数名2 : ' 参数值2 ' 

      }

})

②($route.params.参数名  来接收传过来的参数)

this.$router.push ({

      path : ' /路径/参数值 ' ,

})

 

方法二: name 命名路由的方式跳转(适合 path 路径长的场景)

① 给路由起名字:

const router = new VueRouter({
  routes: [
    { name : ' haha ' ,  path : ' /find ' ,  component : Find },
  ],
})

② 按钮的点击事件中写:

this.$router.push ({

      name : ' 路由名字 '   (name : ' haha ')

})