question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Router can no longer be optionally injected

See original GitHub issue

Which @angular/* package(s) are the source of the bug?

router

Is this a regression?

Yes

Description

This was broken in #46914 (released in v14.1.0).

You can no longer optionally inject a Router service:

  constructor(@Optional() private router: Router) { }

without importing RouterModule (or RouterTestingModule) results into:

ERROR NullInjectorError: R3InjectorError(AppModule)[Router -> UrlSerializer -> UrlSerializer -> UrlSerializer]: 
  NullInjectorError: No provider for UrlSerializer!

Our use case is that we have some general purpose components which have different logic based on the existence of the router.

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 14.1.1
Node: 16.15.1
Package Manager: npm 8.11.0
OS: darwin arm64

Angular: 14.1.1
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1401.1
@angular-devkit/build-angular   14.1.1
@angular-devkit/core            14.1.1
@angular-devkit/schematics      14.1.1
@schematics/angular             14.1.1
rxjs                            7.5.6
typescript                      4.7.4

Anything else?

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
atscottcommented, Aug 5, 2022

Note that the error was a legitimate issue and is fixed by https://github.com/angular/angular/commit/c7fed38cf0fbf072182ec666415ea50f8e84f7f7

However, that isn’t really the problem you’re trying to work around here since you’ll just always get a Router when you attempt to inject it. Another option would be to optionally inject ActivatedRoute instead. That would work immediately for you, but it’s still relying on the details of the Router/RouterModule implementation that I don’t think is really the right solution.

0reactions
angular-automatic-lock-bot[bot]commented, Sep 10, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 9 "Error: This constructor was not compatible with ...
The router class itself is just a normal class with no @Injectable() decorator. Share.
Read more >
Why an NSSA ABR does not inject a default route ...
In this topology if an ASBR inject a default route into OSPF in order to permit internal ospf routers to reach another routing...
Read more >
Dependency injection creates new instance each time if ...
My application is divided into modules. Router navigates between modules, each child modules has some providers and some components. I expect ...
Read more >
BGP Conditional Route Injection | Configuration
BGP Conditional Route Injection is a technique that injects a route based on a condition and the already existing route.
Read more >
Avoiding Startup service injection in ASP.NET Core 3
In this post I describe the changes to dependency injection in your Startup class when upgrading an ASP.NET Core 2.x app to .NET...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found