Angular Dart: matching rules for route path - implicit suffix wildcard?

限于喜欢 提交于 2019-12-10 21:56:04

问题


Consider (excerpt from the AngularDart tutorial):

router.root
  ..addRoute(
      name: 'add',
      path: '/add',
      enter: view('view/addRecipe.html'))

How is a URL matched with such a path? Is there an implicit wildcard suffix like /add/* or maybe /add*? If so, how can I make /add match exactly /add to avoid conflicts with, say, /address?


回答1:


Correct, UrlTemplate does a naive prefix match, so /add will match /address.

If you are worried about conflicts between two routes where path of one happens to be a prefix of another, then the correct approach is to put the most specific path first. For example:

router.root
  ..addRoute(
      name: 'address',
      path: '/address',
      enter: view('view/address.html'))
  ..addRoute(
      name: 'add',
      path: '/add',
      enter: view('view/addRecipe.html'))

Router matches routes in the order they are specified, so it will pick the first that matches. This way /address will always match address route and /add will always match add route.

If you are worried about unintended matches of /addFoo to /add, at the moment I'm afraid there's no easy way to ensure that. If you feel strongly about it please file a feature request against the route_hierarchical package.




回答2:


If you check out the source code (client.dart in route_hierarchial package, which in turn is used by AngularDart) you will notice that path is used as a key into a map. This means that if your path is set to /add it will not match /address.



来源:https://stackoverflow.com/questions/21463238/angular-dart-matching-rules-for-route-path-implicit-suffix-wildcard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!