Skip to content

Allow to pass data with router.navigate #25658

Closed

Description

@earshinov

I'm submitting a...

[x] Feature request

Current behavior

Currently Angular does not provide a way of passing additional data with router.navigate.

There are many use cases for this, described in related issues which were unjustly closed without fulfilment:

#10248 is not actually closed, but addresses only a part of the problem.

Expected behavior

Here is an example how it could work taken from #5217:

// exactly like @RouteConfig
this.router.navigateByUrl('/path', { data: { entity: 'entity' } })

The given data should be accessible everywhere where route.data is accessible (in the target component, resolvers if any etc.)

Environment

Angular version: from 2 up to the current.

Additional Information

There is a workaround by @hipparch:
https://github.com/Hipparch/Angular2-navigate-with-data

Activity

  1. tmburnell commented on Aug 24, 2018

    @tmburnell

    It is funny because i was just looking at this today. The only way i could see to pass data was via route which is static in the router config ... so i created this hacked solution:

    let route = this.router.config.find(r => r.path === '/path');
    route.data = { entity: 'entity' };
    this.router.navigateByUrl('/path');
    

    This works the only issue i saw is if you refresh your screen it does not persist the data.

  2. dawidgarus commented on Aug 25, 2018

    @dawidgarus

    @earshinov @tmburnell I like the idea of passing arbitrary data, but it think they should be passed as route params and resolved via resolvers.
    For example, You want to go to page with item details but You already have the item object, so You navigate to /item/:id and pass it as param. Then in resolver You could use the object in params to resolve data or use id param to download it or navigate to error page. The thing is, You should take care of situation when user clicks refresh button in browser and all the params that are not in url will be lost.

    EDIT: I think the most common use case for this would be forms. You could easily create multi page forms or use it to fill forms with data You already have, like when You navigate from item details to item modification form.

  3. tmburnell commented on Aug 27, 2018

    @tmburnell

    @dawidgarus I also wanted to try and use a URL state (that would allow for refresh)... but could not find API's that would allow this to work.
    The I had for not putting it in the URL is for something like E-commerce (or something restrictive) I dont want to make it deep linkable. I want to force a user to come from somewhere (within the app) not just click a link.

  4. earshinov commented on Aug 29, 2018

    @earshinov
    Author

    Guys, for those of you who are looking for ways to persist the state, please follow #10248. That task is already in progress and hopefully you will have this feature available soon.

  5. dawidgarus commented on Aug 29, 2018

    @dawidgarus

    @earshinov I don't think that history state is a way to go. There is one state for all pages and it's not preserved if You navigate to a subpage. For example, You go to /foo and set some data in history state that could be retrieved by FooComponent attached to route /foo. Then You go to /foo/bar and then to /baz. When You go back to /foo/bar, FooComponent can't retrieve the data. That's why the data should be attached to angular's route.

  6. earshinov commented on Aug 29, 2018

    @earshinov
    Author

    @dawidgarus I might be mistaken, but there is a widely accepted solution for your scenario--storing the state in a service provided in the /foo component.

  7. dawidgarus commented on Aug 29, 2018

    @dawidgarus

    @earshinov That won't for the scenario from my previous comment. Service will be destroyed when You navigate from /foo/bar to /baz

  8. michaelz commented on Aug 30, 2018

    @michaelz

    @earshinov you may want to have a look at this comment #10248 (comment)

  9. earshinov commented on Aug 30, 2018

    @earshinov
    Author

    @dawidgarus, In that case I would provide the service in the root component module then.

    @michaelz, The comment you referenced is about persisting the data—a topic I would prefer to be discussed solely in #10248 and not brought up in this ticket.

    I understand that I might be missing the whole picture, but on the other hand I'd like to have this ticket neet, tidy, understandable and focused on the one fairly simple scenario I've given in the ticket description.

  10. dawidgarus commented on Aug 31, 2018

    @dawidgarus

    @earshinov Another bad idea. You could return to previous page, but when You navigate to the same page all the information will be reset, so You can't go back: /foo (saved data for this page in root service is lost) -> /bar -> /foo

  11. added this to the needsTriage milestone on Sep 4, 2018
  12. andreaslarssen commented on Sep 5, 2018

    @andreaslarssen

    +1

  13. jasonaden commented on Nov 20, 2018

    @jasonaden
    Contributor

    @earshinov @dawidgarus @andreaslarssen @tmburnell @michaelz

    Thanks for the issue all. See the PR #27198 allowing you to set history.state on navigation. This state object will be available to the currently executing navigation. We are finalizing how this API will look, but this should land very shortly and hopefully will satisfy what you're looking for.

    If you have comments on the PR, please add them there rather than on this issue as I will see them quicker on the PR.

  14. added
    feature
    on Nov 20, 2018
  15. 4 remaining items

  16. dawidgarus commented on Dec 4, 2018

    @dawidgarus

    @jasonaden the solution with history state doesn't work well with nested and auxiliary routes as I described above - you would need to manually merge the state at each navigation. Also, there's a limit on history state size and you can only pass json objects that can be serialized, which greatly decrease it's usefulness for scenarios above.

  17. earshinov commented on Dec 4, 2018

    @earshinov
    Author

    @jasonaden , I share @dawidgarus's opinion that the fix does not cover all cases. The case I had in mind when I was filing the ticket (perhaps, different from @dawidgarus's) is as follows:

    1. There is a navigable component A fetching some data from the backend.
    2. There is a second component B that sometimes navigates to A and happen to have all the data A need.
    3. If there was a way to pass data with router.navigate, B could pass the data to A so that A does not need to make an excessive request to the backend.

    Note that in my case passing additional data serves only optimization purposes. It does not cancel the fact that A must be able to fetch the data itself when the user navigates to it directly rather than via component B.

    We are currently using https://github.com/Hipparch/Angular2-navigate-with-data.

  18. Airblader commented on Dec 16, 2018

    @Airblader
    Contributor

    As I pointed out in #27198, as a user I would greatly appreciate some documentation regarding the lifecycle of a single navigation as it's unclear to me "how long" a navigation is on-going and thus how long I can access this data.

    From a quick test it seems that

    this.route.queryParamMap.subscribe(() => console.log(this.router.getCurrentNavigation().extras.state));
    

    contains the state, but is this guaranteed? When does queryParamMap emit?

  19. RamItDown commented on Jan 21, 2019

    @RamItDown

    I don't understand, why some solutions (which were available earlier) need to be proved that are needed... I know that some things can be made other way, but what for, when such solution was the best one...
    Tooday I just needed exactly such case when I wanted to use such option. Because I use data in routing definition, where set some const params, aadditionally some data set be resolever, and neede add extra data in case when someone is using for navigation specific buttons, and this is the perfect example, where passing data by extending snapshot data params would be the best way... and I can't, becouse..?
    I have to install additionall solutions tu support functionalities which has been removed from previous version of framework...?

  20. Airblader commented on Feb 2, 2019

    @Airblader
    Contributor

    @jasonaden I think this issue can be closed now since passing state landed in 7.2.0?

  21. earshinov commented on Feb 3, 2019

    @earshinov
    Author

    @Airblader , did you care to read my and @dawidgarus's comments?

  22. Airblader commented on Feb 3, 2019

    @Airblader
    Contributor

    I actually didn't read them, sorry. I just randomly came across this issue when looking up something else.

    The case you described sounds like a basic state management task to me. Either have a service that lazily fetches the data or use a more sophisticated tool like ngrx. Why doesn't that work?

    In any case if this is still an open question then of course the core team should respond to it first.

  23. earshinov commented on Feb 3, 2019

    @earshinov
    Author

    It works, but making a separate service with the sole purpose of being a temporary state holder / transfer-er feels an awkward and unnecessary complication.

    Just so you know, you are not the first to suggest services, see tickets I linked in the description.

  24. Airblader commented on Feb 3, 2019

    @Airblader
    Contributor

    I would argue that passing data around in the navigation everywhere makes for a complex, hard to reason with architecture that won't scale.

    But I'm not really here to argue any of that. I randomly stumbled across this issue and thought to point out that it appeared to be closeable. I was wrong about that, I didn't mean to undermine remaining open questions.

  25. added
    feature: under consideration
    on Jun 4, 2021
  26. atscott commented on Apr 26, 2024

    @atscott
    Contributor

    This was actually resolved by 5c1d441. You can access the info in several ways:

    • During the navigation, such as in guards and resolvers, with router.getCurrentNavigation().extras.info
    • After the navigation with router.lastSuccessfulNavigation.extras.info
    • Through ActivatedRoute.data if you get the information in a resolver
  27. angular-automatic-lock-bot commented on May 27, 2024

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  28. locked and limited conversation to collaborators on May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: routerfeaturefeature: under considerationfreq3: high

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions