Angular Dart: matching rules for route path - impl

2019-07-04 01:07发布

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?

2条回答
虎瘦雄心在
2楼-- · 2019-07-04 01:38

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.

查看更多
smile是对你的礼貌
3楼-- · 2019-07-04 01:44

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.

查看更多
登录 后发表回答