Vue路由传参的两种方式

163 阅读1分钟

 在vue中,参数传递共有2种:

http://localhost:8080/demo?cid=c001

http://localhost:8080/demo2/c001

查询参数

  • 确定访问路径

    

    <router-link to="/demo?cid=c001">Demo |

    <router-link :to="{ path: '/demo', query: { cid: 'c001' }}">demo 

  • 编写路由

  {

    path: '/demo',

    name: 'demo',

    component: Demo

  },

  • 编写页面,并获得参数

路径参数

  • 编写访问路径

    <router-link :to="{ name: 'demoName2', params: { uid: 123 }}">Demo2 |

    <router-link to="/demo2/123">Demo2 |

  • 确定访问路径

  {

    path: '/demo2/:uid',

    name: 'demoName2',

    component: Demo2

  }

  • 编写路由

{

    path: '/demo2/:uid',

    name: 'demoName2',

    component: Demo2

  } 

  • 编写页面,并获得参数

路由总结

  • 在任何组件内通过 this.$router 访问路由器
  • 在任何组件内通过 this.$route 访问当前路由:
this.$route相关操作描述实例
this.$route.params获得路径参数模式       /user/:uid
匹配路径/user/123
this.$route.params.uid123
this.$route.query获得请求参数/user?id=123