angular icon indicating copy to clipboard operation
angular copied to clipboard

RoutePath compare by value

Open adamlofts opened this issue 6 years ago • 2 comments

I have a component routed by 2 different RoutePath objects. I want to check which route is used in onActivate with code like this:


@override
  void onActivate(RouterState previous, RouterState current) {
  if (current.routePath == RoutePaths.a) {
    // Do something
  } else if (current.routePath == RoutePaths.b) {
    // Do something else
  }
}

This doesn't work because the current instance is different to RoutePaths.a (for example) and RoutePath does not implement the equality operator.

adamlofts avatar May 16 '19 12:05 adamlofts

RoutePath used to implement operator == to support such comparisons, but it was removed in favor of better tree-shaking since it wasn't widely used. The issue with operator == is that once you use a == comparison elsewhere in your app, all implementations are retained in the release build (even if they're never used by a == comparison).

We may reevaluate this again, but for now you could just compare the URL values:

if (current.routePath.toUrl() == RoutePaths.a.toUrl()) {
  ...
}

leonsenft avatar May 17 '19 20:05 leonsenft

Makes sense. Maybe a good argument for language support of value types? Anyway feel free to close the ticket if you don't need it.

adamlofts avatar May 20 '19 07:05 adamlofts