Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to override provider? #22

Open
sebestindragos opened this issue Apr 27, 2017 · 1 comment
Open

How to override provider? #22

sebestindragos opened this issue Apr 27, 2017 · 1 comment

Comments

@sebestindragos
Copy link

sebestindragos commented Apr 27, 2017

Hi! I have a service in my app that makes use of localStorage. I tried making use of provide with an InjectToken to override it on the server, but I can't think of a solution on how to do it.

AppModule looks like this:

imports: [
    BrowserModule.withServerTransition({
      appId: 'app'
    }),
],
providers: [
  {provide: LocalStorage, useValue: window.localStorage}
]

LocalStorage is an InjectToken instance
export const LocalStorage = new InjectToken();

and in my server side code I have

app.engine('html', ngExpressEngine({
  baseUrl: nconf.get('hostname'),
  providers: [
     {provide: LocalStorage, useValue: {
      setItem() {},
      getItem() {},
      removeItem() {}
    }}
  ],
  bootstrap: [AppServerModuleNgFactory],
}));

ngExpressEngine

renderModuleFactory(setupOptions.bootstrap[0], {
  document: templateCache[filePath],
  url: options.req.url,
  extraProviders: setupOptions.providers
}).then(template => callback(null, template));

Running the app with this config, doesn't work, and I have no idea if this is even possible.
Can providers be overriden from an outside module?
Documentation seems to be pretty lacking on this subject.

The only way I can think of now, is to not import AppModule in AppServerModule, and
make the AppServerModule basically a copy of AppModule with different imports and providers.
But I'd like to avoid this method.

Any ideas?
Thanks!

@kkaabbaa
Copy link

I had problems with localStorage. I solved them only so

if (typeof window! == 'undefined') {
        const token = localStorage.getItem ('token');
     }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants