Hi Angular:
I am circling back to a problem I encountered a while ago and didn't have
time to resolve. The story is that I have a angular 1.7 app with a rather
eccentric setup. (It runs as both standalone website and a plugin for
another angular app with different bootstrap code path)
I wanted to upgrade our dependencies and part of this implies I will have
to start using angular 7. However, the static injector is starting to give
me issues.
The SDK I use requires me to instantiate services (from their library) and
pass it to its application context during app init. I need to do something
as followed.
class AppComponent implements OnDestroy, OnInit {
constructor(
private appContext: AppContextService, private navigationService:
NavigationService) {
}
public ngOnInit(): void {
this.appContext.ngInit({ navigationService: this.navigationService });
}
}
Where AppContextService and NavigationService are both provided by the SDK.
However, after upgrading to angular 7 I am starting to see this error
during my app launch.
(object): Error: StaticInjectorError(AppModule)[NavigationService ->
Router]:
StaticInjectorError(Platform: core)[NavigationService -> Router]:
NullInjectorError: No provider for Router!
Note that NavigationService is dependent of @angular/router and I have
RouterModule.forRoot in my app module, also I remembered to export
RouterModule in my app module.
This is extremely confusing because I actually tried to move the
instantiation of NavigationService to the init method and see the behavior
as followed
constructor(
private injector: Injector,
private appContext: AppContextService,
) {
.....
}
public ngOnInit(): void {
let router: Router = this.injector.get(Router);
if (!router) {
console.warn(`no router`);
} else {
console.warn(`yes router`);
}
let navigationService: NavigationService = this.injector.get(
NavigationService);
this.appContext.ngInit({ navigationService: navigationService });
....
Output
*yes router*
*(object): Error: StaticInjectorError(AppModule)[NavigationService ->
Router]: StaticInjectorError(Platform: core)[NavigationService ->
Router]: NullInjectorError: No provider for Router!*
How is this even possible?
(Apologies for asking such a vague question. I am currently trying to
create a minimal reproduction with very little luck due to the complicated
way our app is set up.) Just wondering if anyone can spot the issue or
provide some information that I can work on while I try to come up with a
more concise problem description)
--
You received this message because you are subscribed to the Google Groups
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.