angular-component / router

Angular Component Router - A declarative router for Angular applications
MIT License
253 stars 16 forks source link

usage in effects / ofRoute operator #69

Open fxck opened 3 years ago

fxck commented 3 years ago

Would something like this be a bad idea? And is there some built-in function that would be able to check if route matches pattern (I imagine link active has to be doing something like that).

private action$ = createEffect(() => this._router.url$.pipe(
  // url is /products/detail/123
  filter((url) => ofRoute('/products/detail/:id', url)),
  tap(console.log)
);
brandonroberts commented 3 years ago

There isn't a built in function that does this for the entire route, as each segment is parsed at each level in the component hierarchy. Maybe if the effect was tied to the component?

The ofRoute() would match as soon as the location changes, but would potentially happen before the component is rendered. If you're keeping things reactive if should be fine though.

fxck commented 3 years ago

The ofRoute() would match as soon as the location changes, but would potentially happen before the component is rendered. If you're keeping things reactive if should be fine though.

yea that would be fine in this case, you visit product/123 and you want to check if 123 data already exists and dispatch load action otherwise, I could always dispatch the action from the page component, but I'd rather leave it to the effect

meeroslav commented 3 years ago

With implementation of custom route matched (currently in PR) this would be possible even with the service, but you would have to manually run the checks that components do.

As Brandon said, it's best to use components. We didn't implement public route change events yet.