Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Angular 2 Universal - Store Global Variables #547

Closed

Description

@FahadMullaji

I am trying to convert my AngularJS application to Angular 2 Universal application (because of server-side rendering).

https://github.com/angular/universal-starter

Now I need to store global variables which could be achieved easily in normal Angular 2 applications(per below links).

Angular 2 global variable -
http://stackoverflow.com/questions/37698437/angular-2-global-variable

Angular 2 - Whats the best way to store global variables like authentication token so all classes have access to them?-
http://stackoverflow.com/questions/33598153/angular-2-whats-the-best-way-to-store-global-variables-like-authentication-tok

One thing you will have to do to achieve this in normal angular 2 App is to pass your global service to bootstrap.

I don't think you can do that in Angular 2 universal app unless I am missing something.

I want to store user data and use it across application. Just like Session data in asp.net. And plus this needs to done without making parent-child components

How can I store global variables in Angular 2 Universal App?

Thanks Fahad Mullaji

Activity

  1. MarkPieszak commented on Sep 25, 2016

    @MarkPieszak
    Contributor

    You could store it in a Root level Service like normal, and have all your http calls default to passing the Authorization Token to headers. Did you get some type of error when you tried that?
    @FahadMullaji

  2. FahadMullaji commented on Oct 3, 2016

    @FahadMullaji
    Author

    I fixed this problem by using angular2-cookies package.
    So it was more of a work around than fixing this problem. I was trying store user information so I guess cookies was anyway a better approach.

  3. jeffwhelpley commented on Oct 3, 2016

    @jeffwhelpley
    Contributor

    If you want to store info on a request basis, use service at root module. If you want to store something across multiple requests for a particular user, store ID in cookie for auth and then have the data in redis or somewhere similar on the server side (or just everything in the cookie if it is a small amount of data).

  4. FahadMullaji commented on Oct 12, 2016

    @FahadMullaji
    Author

    Ok. so I tried my workaround and it worked and it solved my problem but now there is another problem.
    Whenever I use cookies to pull data then system throws exception saying "Document is not defined". And then system does not do server side rendering. Which is a huge deal breaker.

    So now I need a cookies module for universal which works with serverside rendering.

  5. scott-wyatt commented on Oct 12, 2016

    @scott-wyatt

    @FahadMullaji there are a lot of open issues for cookies #404 #268 to name a few.

  6. angular-automatic-lock-bot commented on Sep 4, 2019

    @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.

  7. locked and limited conversation to collaborators on Sep 4, 2019
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions