Universal: Injecting REQUEST/RESPONSE tokens throws errors in AoT

Created on 13 May 2017  路  9Comments  路  Source: angular/universal

  • I'm submitting a ...
  • [x] bug report
  • [ ] feature request
  • [ ] support request => Please do not submit support request here, see note at the top of this template.

  • What modules are related to this Issue?

  • [x] express-engine
  • [ ] aspnetcore-engine

  • What is the current behavior?
    Injecting REQUEST or RESPONSE throws the following error during an AoT build:
    Error encountered resolving symbol values statically. Could not resolve @nguniversal/express-engine/tokens

I've seen this when ngc encounters a static value, usually exported as a const, rather than a function(). I was able to fix it by doing the following in ng-express-engine/src/tokens.ts:

export function REQUEST() { return new InjectionToken<Request>('REQUEST'); }
export function RESPONSE() { return new InjectionToken<Response>('RESPONSE'); }

and then consuming them in my app by invoking those functions as follows:

import { REQUEST } from '@nguniversal/express-engine/tokens';
constructor(@Inject(REQUEST()) req: any) {}

I personally think is ugly, but it works in AoT mode :man_shrugging:
I could make a PR if you guys think it's a good idea, but please let me know if anyone has a better solution.

  • What is the expected behavior?
    Should not receive the above error when injecting REQUEST or RESPONSE tokens
express-engine

Most helpful comment

I have been able to get around this by injecting the Injector and manually requesting it via .get()

// Example:
import { REQUEST } from '@nguniversal/express-engine/tokens';
import { Injectable, Injector } from '@angular/core';

@Injectable()
export class MyService { 
  constructor(private injector: Injector) { 
    const req = this.injector.get(REQUEST);
    console.log('We have the object!');
    console.log(req);
  }
}



All 9 comments

There must be a prettier solution but i can't seem to find out what it is.

I feel like it must be packaging or something because we use the exact same code outside of the module.

any new developments on this issue? or a way to get around it? Thanks!

I'd be interested too ! The solution from @yazeedb doesn't compile for me in AOT neither.. I get a no provider for InjectionToken MYTOKEN..

This compiles in AOT but when you load the page it does say, as @julienR2 said, No provider for InjectionToken REQUEST!

Thanks !

I have been able to get around this by injecting the Injector and manually requesting it via .get()

// Example:
import { REQUEST } from '@nguniversal/express-engine/tokens';
import { Injectable, Injector } from '@angular/core';

@Injectable()
export class MyService { 
  constructor(private injector: Injector) { 
    const req = this.injector.get(REQUEST);
    console.log('We have the object!');
    console.log(req);
  }
}



We can close this via https://github.com/angular/universal/pull/719

It has already been published, this should not be a problem with 1.0.0-beta.2

done via #719

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

inorganik picture inorganik  路  4Comments

dottodot picture dottodot  路  6Comments

ahmedwerpx picture ahmedwerpx  路  4Comments

benrondeau picture benrondeau  路  5Comments

flexchintoo picture flexchintoo  路  5Comments