Vue中this.$router.push参数获取

传递参数的方法:
1.Params

由于动态路由也是传递params的,所以在 this.$router.push() 方法中path不能和params一起使用,否则params将无效。需要用name来指定页面。

及通过路由配置的name属性访问

在路由配置文件中定义参数:

通过name获取页面,传递params:

在目标页面通过this.$route.params获取参数:

2.Query

页面通过path和query传递参数,该实例中row为某行表格数据

在目标页面通过this.$route.query获取参数:

this.$route.query.row.xxx

vue-router 2.0 常用基础知识点之router.push()    https://blog.csdn.net/sinat_17775997/article/details/68941091

router.push(location)     

http://www.jianshu.com/p/ee7ff3d1d93d

除了使用 <router-link> 创建 a 标签来定义导航链接,我们还可以借助 router 的实例方法,通过编写代码来实现。
router.push(location)
想要导航到不同的 URL,则使用 router.push 方法。这个方法会向 history 栈添加一个新的记录,所以,当用户点击浏览器后退按钮时,则回到之前的 URL。

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

声明式:<router-link :to="...">
编程式:router.push(...)
该方法的参数可以是一个字符串路径,或者一个描述地址的对象。

 
  1. // 字符串

  2. router.push('home')

  3.  
  4. // 对象

  5. this.$router.push({path: '/login?url=' + this.$route.path});

  6.  
  7. // 命名的路由

  8. router.push({ name: 'user', params: { userId: 123 }})

  9.  
  10. // 带查询参数,变成/backend/order?selected=2

  11. this.$router.push({path: '/backend/order', query: {selected: "2"}});

  12.  
  13. // 设置查询参数

  14. this.$http.post('v1/user/select-stage', {stage: stage})

  15. .then(({data: {code, content}}) => {

  16. if (code === 0) {

  17. // 对象

  18. this.$router.push({path: '/home'});

  19. }else if(code === 10){

  20. // 带查询参数,变成/login?stage=stage

  21. this.$router.push({path: '/login', query:{stage: stage}});

  22. }

  23. });

  24.  
  25. // 设计查询参数对象

  26. let queryData = {};

  27. if (this.$route.query.stage) {

  28. queryData.stage = this.$route.query.stage;

  29. }

  30. if (this.$route.query.url) {

  31. queryData.url = this.$route.query.url;

  32. }

  33. this.$router.push({path: '/my/profile', query: queryData});

replace

类型: boolean
默认值: false
设置 replace 属性的话,当点击时,会调用 router.replace() 而不是 router.push(),于是导航后不会留下 history 记录。即使点击返回按钮也不会回到这个页面。
//加上replace: true后,它不会向 history 添加新记录,而是跟它的方法名一样 —— 替换掉当前的 history 记录。

 
  1. this.$router.push({path: '/home', replace: true})

  2. //如果是声明式就是像下面这样写:

  3. <router-link :to="..." replace></router-link>

  4. // 编程式:

  5. router.replace(...)

综合案例

this.$router.push({path: '/coach/' + this.$route.params.id, query: queryData});

猜你喜欢

转载自blog.csdn.net/thinkingw770s/article/details/84441896