angular-component / router

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

feat(router): provide access to all route params via router service. resolves #39 #47

Closed meeroslav closed 4 years ago

meeroslav commented 4 years ago

Problem:

Expose route params from the router component back to router service to have it available for all components.

The tricky part:

Since routes are not configured in central configuration (like in @angular/router) but rather loaded and parsed asynchronously from components, the routeParams will change until parsing has been fully finished.

Unrelated bug fixed separately (found in router component and service):

distinctUntilChanged does value referential comparison. It works nicely for strings, but for objects (such as Params) a custom comparison function must be provided e.g.:

const paramsCompare = (x,y) = x === y || JSON.stringify(x) === JSON.stringify(y)
meeroslav commented 4 years ago

@brandonroberts please verify if you agree with this functionality

brandonroberts commented 4 years ago

How does this handle naming collisions? I want to keep the route params relatively straightforward, and lean more on using the components to pass data around then trying to collect them all in the router service.

meeroslav commented 4 years ago

Currently, the child param has priority over the parent param:

// root params -> child params -> child params => pathRoutes
{ owner: 'Joe' } -> { car: 'Mustang' } -> { owner: 'Jane' } => { owner: 'Jane', car: 'Mustang' }

My rationale was to provide as many features as possible from the official @angular/router to motivate devs to migrate. Two of those features missing are:

With the original router, we can access easily all path params (should a component need it). The current solution with angular-routing is to depend on parent components to pass it down to the child component. This leads to a rigid structure and complications when the component is moved around through DOM.

Back to name handling, we could return Params[] instead of Params. That would avoid naming collision and perhaps also provide more sense of hierarchy of params.

What do you think?

brandonroberts commented 4 years ago

We discussed tabling this feature for now, but will revisit