Dynamic Route Matching

动态的 路由 匹配

Very often we will need to map routes with the given pattern to the same component. For example we may have a User component which should be rendered for all users but with different user IDs. In vue-router we can use a dynamic segment in the path to achieve that:

需要通过给定的模式去导航路由到相同的模块,比如我们有一个用户组件,它被每个用户所使用,但是每个用户有不同的id。在vue 路由中,我们可以使用一个动态的部分 来实现它。

const User = {
template: '<div>User</div>'
} const router = new VueRouter({
routes: [
// dynamic segments start with a colon
{ path: '/user/:id', component: User }
]
})

  

Now URLs like /user/foo and /user/bar will both map to the same route.

现在 像/user/foo 和 /user/bar 的URLS 将被同时导航到 相同的 地方

A dynamic segment is denoted by a colon :. When a route is matched, the value of the dynamic segments will be exposed as this.$route.params in every component. Therefore, we can render the current user ID by updating User's template to this:

一个动态的部分被: 标志, 当一个路由相匹配时,动态变量的值将会被暴露为this.$route.params,在每一个组件里。因此,我们可以填充当前的用户id通过更新用户组件成这样:

const User = {
template: '<div>User {{ $route.params.id }}</div>'
}

You can have multiple dynamic segments in the same route, and they will map to corresponding fields on $route.params. Examples:

你可以有很多多样的的动态变量在同一个路由里面,他们将映射到对应的部分,在$route.params

pattern matched path $route.params
/user/:username /user/evan { username: 'evan' }
/user/:username/post/:post_id /user/evan/post/123 { username: 'evan', post_id: '123' }

In addition to $route.params, the $route object also exposes other useful information such as $route.query (if there is a query in the URL), $route.hash.etc. You can check out the full details in the API Reference

除了 $route.params, $route 对象还暴露出其他的有用信息,比如 路由?,路由的哈希,等等。你可以查询所有的细节在api参考里。

最新文章

  1. SQL SERVER中什么情况会导致索引查找变成索引扫描
  2. 代码重构之 —— 一堆if、esle 逻辑的处理
  3. underscorejs
  4. Awesome Machine Learning
  5. Ubuntu 中搭建 LAMP 及 php 开发工具
  6. JS面相对象
  7. 【转载】简述Linux的启动过程
  8. 一道关于比赛胜负的Sql查询题目
  9. WebSocket原理及与http1.0/1.1 long poll和 ajax轮询的区别【转自知乎】
  10. android之自定义ViewGroup和自动换行的布局的实现
  11. codevs 1421 秋静叶&amp;秋穣子(树上DP+博弈)
  12. QT中LineEdit、TextEdit 、PlainTextEdit 三个控件的区别
  13. 消除SDK更新时的“https://dl-ssl.google.com refused”异常--(转)
  14. Scrapy爬取西刺代理ip流程
  15. tp5.0 queue 队列操作
  16. python pymysql.err.InternalError: (1366, &quot;Incorrect string value: &#39;\\xE9\\x9F\\xA9\\xE6\\xA2\\x85...
  17. Android中碎片的添加问题
  18. 深入理解JS函数中this指针的指向
  19. python-GIL、死锁递归锁及线程补充
  20. [UE4]小地图UI放在哪里创建合适?

热门文章

  1. ODT珂朵莉树
  2. Django 3.0中不推荐使用的及已经删除的功能
  3. 「NOIP2015」运输计划
  4. 解决vmware 桥联 再次使用联不上网的问题
  5. NPM概述及使用简介
  6. js 对象补充
  7. MySQL查询事务 杀死事务
  8. es678910语法糖
  9. 014.Oracle数据库,取本周第一天(星期一), 取本周最后一天(星期天)
  10. JSON数组序列化C#方法