Browser back button not working wtih Standalone components

This issue has been created since 2022-11-20.

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

router

Is this a regression?

Yes

Description

I upgraded my personal project and saw that browser back button stopped working with standalone components. It works fine if used with NgModule.This bug is present in both V14 and V15.

Stackblitz( Standalone ) :- https://stackblitz.com/edit/angular-ivy-mhlv5j?file=src/app/landing.component.ts

Stackblitz ( NgModule) :- https://stackblitz.com/edit/angular-ivy-kp1ttk?file=src/app/app.module.ts

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-ivy-mhlv5j?file=src/app/landing.component.ts

Please provide the exception or error you saw

The browser URL changes but component does not change.

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

@angular/animations 14.0.0

@angular/common 14.0.0

@angular/compiler 14.0.0

@angular/core 14.0.0

@angular/forms 14.0.0

@angular/platform-browser 14.0.0

@angular/platform-browser-dynamic 14.0.0

@angular/router 14.0.0

rxjs 7.5.5

tslib 2.4.0

zone.js 0.11.5

Anything else?

No response

JoostK wrote this answer on 2022-11-20

You're using the API incorrectly with the setup in Stackblitz. The router should be configured as environment provider, whereas you're using RouterModule.forRoot only from an NgModule that is imported using Component.imports. This results in the router providers not being configured in the environment injector, but in the component instead.

Updating the StackBlitz to v15 reports an error for this setup:

Error: NG04007: The Router was provided more than once. This can happen if 'forRoot' is used outside of the root injector. Lazy loaded modules should use RouterModule.forChild() instead.

The proper way to make this work is to change main.ts using

bootstrapApplication(AppComponent, {
  providers: [importProvidersFrom(AppRoutingModule)]
})

and remove AppRoutingModule from Component.imports in AppComponent.

Confusion around environment providers vs component providers is a problem we are aware of and newly designed APIs like provideRouter result in a type-check error when using it incorrectly like in this case, so I'm closing this as not further actionable.

More Details About Repo
Owner Name angular
Repo Name angular
Full Name angular/angular
Language TypeScript
Created Date 2014-09-18
Updated Date 2022-12-01
Star Count 85220
Watcher Count 3042
Fork Count 22607
Issue Count 1316

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Can't Deploy the Active Passive SDN 2 2022-04-14 2022-10-23
Route to 168.63.129.16 1 2022-04-13 2022-10-23
I am getting minimal readership error while trying to submit my website URL 3 2019-05-28 2022-11-23
In REPL: Ctrl + Z or M will be a line feed, Ctrl + I will be like typing a Tab, Ctrl + H will delete the previous character, Ctrl + J will do nothing 1 2022-08-18 2022-10-27
Bot not available in group chat 2 2021-08-13 2022-11-18
Consider whether collection entries should emphasize version or entry info 0 2022-07-28 2022-09-24
APIVersion not showing up on Describe 0 2022-09-29 2022-09-30
evaluate.py compute_losses? 0 2021-05-08 2022-11-28
Add the capability to automatically detect if OneNote is installed for all the users or just for the current one and remove that from the OneMore installer 2 2022-01-10 2022-11-28
Translation questions 5 2021-10-07 2022-10-30
Better as a Clojure tool using tools.deps.alpha? 4 2021-11-19 2022-11-21
[FEA] highlight pattern matches and submatches 0 2022-10-13 2022-11-05
Provide OpenAPI specs 0 2021-03-17 2022-10-22
Concurrency Control in DBMS 1 2022-02-09 2022-08-18
axe not compatible with Content-Security-Policy (CSP) 3 2021-11-18 2022-10-16
ALPN server did not agree to protocol...when I try to make from source 2 2021-08-22 2022-11-29
[1.18] Crash when resetting all gameplay settings in configuration menu 0 2021-12-10 2022-09-30
cp: -LP flags cause an error when used together 0 2022-01-31 2022-11-14
Some hooks should get fixed 8 2021-08-13 2022-11-01
off-by-one in archive/zip causes test failure on wasi 9 2022-01-19 2022-11-11
DotStar support for the itsybitsy m0 6 2022-01-28 2022-10-29
After switching to another application, the emulator does not pause: music continues to play, etc. 5 2021-01-09 2022-11-03
Filter by boolean doesnt work 0 2021-09-28 2022-09-25
Videojs 360 Video works on desktop but not mobile 2 2020-07-04 2022-11-17
Blackbox testing: ABI types and @AbiReturnSubroutine round-trip encoding + decoding 1 2022-05-10 2022-08-18
Support OpenSSL 3.0 3 2020-05-17 2022-01-12
[BUG] By default, FlyteDirectory is empty when executing workflow on Flyte cluster 3 2022-08-24 2022-09-14
Migrate HTTP code from pusher to uWebSockets-express or nanoexpress 0 2022-02-15 2022-10-05
The terraform-provider-vcd_v3.3.1 plugin crashed 4 2021-09-14 2022-11-29
Settings up some basic CI for guaranteeing build-pass 1 2021-02-26 2022-10-29
`dunai`: instance of `VectorSpace` does not implement `dot` 2 2022-06-04 2022-11-02
Feature Request: New Access Server - "LDAP + Radius" 3 2021-10-15 2022-11-03
Improve Test Framework Time With Run Order Directive 1 2021-12-17 2022-10-15
Use the Svg prefix globally 3 2022-01-19 2022-10-18
Feature Request: Spoof touch support API(s) 3 2021-05-03 2022-11-11
Error: Don't set relation directly 1 2021-09-09 2022-11-28
Reprojucer.cmake doesn't support the setting `USE_LEGACY_BUILD_SYSTEM` 0 2022-07-31 2022-11-19
Reprojucer.cmake doesn't support the setting `USE_IPP_LIBRARY_ONE_API` 0 2022-07-31 2022-11-19
Reprojucer.cmake doesn't support the setting `USE_MKL_LIBRARY_ONE_API` 0 2022-07-31 2022-11-19
xmake-vscode improvements 0 2022-03-31 2022-11-22
How to generate a kubernetes friendly yaml from CronJob object? 1 2021-02-16 2022-09-28
Very fast zlib-compatible compression. 0 2020-12-02 2022-09-15
Android build on mac is failing because of JCenter shutdown 1 2021-07-16 2022-11-30
[Patch] Legibility and typos, mark 5 1 2022-09-28 2022-11-18
Login fails when run on a remote machine: PermissionError: [WinError 5] 11 2021-12-11 2022-08-06
fails on az login: RuntimeError: 0. The ID token is not yet valid. 1 2021-12-11 2022-08-29
单元测试 `SM2Test.dLengthTest` 中 `SM2.getD()` 偶尔会报错 2 2022-01-07 2022-10-03
从2022.05.01开始直播间获取不到小心心 0 2022-05-02 2022-11-25
Pixman renderer asserts when using scene graph 8 2021-10-21 2022-11-24
Add Nepali locale `ne` 6 2021-05-18 2022-11-30