CanMatch snapshot queryParams doesn't work

This issue has been created since 2022-09-13.

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

router

Is this a regression?

No

Description

I want a sync access to router.snapshot.queryParams in CanMatch guard and it doesn't work:

{
          path: 'todos',
          canMatch: [() => {
            // the location is todos?foo=1
            console.log(inject(ActivatedRoute).snapshot.queryParams) // { }
            return true;
          }],
}

If I wrap it with a setTimeout it logs the correct value.

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)

No response

Anything else?

No response

JoostK wrote this answer on 2022-09-13

It seems illogical to me that ActivatedRoute would represent the route that is still being matched; it has not yet been activated so ActivatedRoute does not reflect the navigation that is being matched. The CanMatch functions are called with segments: UrlSegment[], is that what you're looking for?

ArielGueta wrote this answer on 2022-09-16

I understand what you're saying, but I expect at least to get access to the "current" activated route.

atscott wrote this answer on 2022-09-21

The current activated route is the current activated route. This is as @JoostK has described. Closing as working as intended.

FYI - you can get what you're looking for with inject(Router).getCurrentNavigation().initialUrl.queryParams

https://stackblitz.com/edit/angular-ivy-sgtr4a?file=src%2Fmain.ts,src%2Findex.html

ArielGueta wrote this answer on 2022-09-21

@atscott thanks! Can you please explain why it called twice, please?

atscott wrote this answer on 2022-09-21

@ArielGueta Yes. It's called twice because there are two stages at the beginning of the routing pipeline that need to call it. One which determines the final URL for the navigation (applyRedirects) and one that creates the ActivatedRouteSnapshots for the navigation (recognize). Both of these match the URL to the Route configs. I'm working on combining these two operations, but it's quite a long process that includes several breaking changes and is being done over the course of several major versions.

ArielGueta wrote this answer on 2022-09-23

Thanks!

More Details About Repo
Owner Name angular
Repo Name angular
Full Name angular/angular
Language TypeScript
Created Date 2014-09-18
Updated Date 2022-09-30
Star Count 84080
Watcher Count 3064
Fork Count 22235
Issue Count 1197

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Secret chats messages not getting delivered to iOS client 8 2022-08-18 2022-09-12
[Bug] Aggregation query failed caused by inconsistent nullability between outputTuple and groupBtExpr 0 2022-07-30 2022-09-21
[1.18] Rework facade system to not require a hard dependency 1 2022-01-01 2022-09-14
Incompatible with Memory Usage Screen 1 2021-07-30 2022-07-02
odoo 13 - Relatórios de contabilidade "balancete, balanço patrimonial " não encontrados, estes items foram retirados? 1 2021-06-12 2022-08-16
Stock Reducer and Composite Product 10 2017-11-15 2022-08-06
Missing folders on first builds 0 2022-01-16 2022-09-11
Update 'Project Listed' email 0 2021-12-22 2021-12-30
Get-AzPasswords - Add Assignable Scope Options for Token Generation 0 2021-07-23 2022-08-14
Timer events or Tasks 2 2017-12-04 2022-08-22
CompactWorkflowSerializer incorrectly deserializes state of Parallel Gateway 2 2018-02-21 2022-08-27
Translate Machine.h for YA_CNC_V1 1 2022-07-28 2022-09-19
Docker images not found wiretrustee/signal and wiretrustee/management 6 2021-08-14 2021-12-30
打包支持输出 source map 3 2021-06-16 2022-01-04
I dont see the option to add a client (firewall) 0 2022-08-02 2022-09-13
Documentation needed on what each metric in the report means 0 2022-05-04 2022-09-08
Distinguish different variants of the same language 2 2022-04-14 2022-09-07
Significant slowdown, exporting to disk 18 2020-09-29 2022-08-13
all image files in "public/system/...." folder are not displayed after cmd "go run main.go". 0 2021-08-22 2022-09-17
TypeError: pretty is not a function 3 2021-09-30 2022-09-21
Shadow DOM compatibility 0 2022-03-14 2022-09-18
Handle NULL in arrays. 2 2017-02-22 2022-09-25
Auto-set moderators 2 2021-11-23 2022-01-22
Check we're using sensible congestion control for full mesh 0 2021-11-23 2022-09-14
Background blurring 0 2021-11-23 2021-12-27
Parsing relative URIs 0 2020-12-29 2022-09-13
Should create Official Docker image for pystan! 25 2020-06-27 2022-09-19
FR: Run .AppImage files regardless of execution permission bit 3 2022-03-26 2022-07-26
[ENH] Sync preview updates 0 2021-10-12 2022-09-17
Windows Debugging: Setting breakpoint results in access violation. 4 2021-07-22 2022-09-23
[meta] where / how to ask for new features / apps etc 1 2021-12-22 2022-09-18
Clarify agent connection error messages during backend instability 4 2021-11-30 2022-09-19
portmidi win32 callbacks can cause a segfault in 64-bit builds 0 2022-01-23 2022-08-30
[CMake] Automated versioning 4 2021-11-21 2022-09-22
Does the OPCUA Publisher accepts NodeIds like this: "Data"."scada"."var"[002] ? 7 2022-09-13 2022-09-22
Missing dependency `libudev` not listed in the README 1 2022-05-15 2022-08-23
[BUG] Azure.Storage.Blob 12.11.0 Exception: InvalidHeaderValue 18 2022-04-26 2022-09-24
Image not rendered in the "Threatened Species" sub-sub-panel 2 2022-01-27 2022-09-07
[Bug] CameraPreview integration with LifecycleManagement 2 2022-01-27 2022-08-12
Missing `usbdef.h` and `usbioctl.h` 2 2021-09-30 2022-09-27
Cypress failed to start. 3 2022-02-04 2022-02-16
`terraform validate` allows invalid backend configuration 3 2022-04-28 2022-09-29
`local-exec` documentation lacks on-destroy documentation 2 2022-04-28 2022-09-18
Init: Support all languages from existing terraform project 0 2022-05-12 2022-09-29
When using the 2.5D view, the "Distance by tap" ruler is displayed in an inconsistent way. 4 2022-06-29 2022-09-26
Push readme to docker.hub 1 2021-12-04 2022-09-24
`function_from_graph_def` failure on a model with a loop after Grappler optimization 2 2022-04-25 2022-09-28
Consistent with() and withCount() behaviour 1 2021-05-11 2022-09-23
add --version flag on cli for xcaddy itself 1 2020-06-30 2022-09-22
Question: Init object by metric coordinates in map_integration 2 2022-06-02 2022-09-21