Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runGuardsAndResolvers documentation doesn’t clearly describe when it’s used #46001

Open
thavus opened this issue May 15, 2022 · 8 comments · May be fixed by #46249
Open

runGuardsAndResolvers documentation doesn’t clearly describe when it’s used #46001

thavus opened this issue May 15, 2022 · 8 comments · May be fixed by #46249
Labels
comp: docs comp: router good first issue P4
Milestone

Comments

@thavus
Copy link

@thavus thavus commented May 15, 2022

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

router

Is this a regression?

No

Description

The Documentation suggests that you can supply a function to runGuardsAndResolvers . The link will take you to the documentation where it gives the type.

So I supplied the following and saw that not only was my route guard still being run, but the console log was not output.

((from: ActivatedRouteSnapshot, to: ActivatedRouteSnapshot) => {
          console.log(from,to);

          return false;
})

It seems something is not working as described in the documentation. So either it's a bug or the documentation is misleading?

Please provide a link to a minimal reproduction of the bug

I don't have time to do this. I'll figure out another solution if needed.

Please provide the exception or error you saw

There were no errors. The guards ran when they should not have.

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

Angular CLI: 13.2.6
Node: 16.14.0
Package Manager: npm 8.3.1
OS: win32 x64

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

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1302.6
@angular-devkit/build-angular   13.2.6
@angular-devkit/core            13.2.6
@angular-devkit/schematics      13.2.6
@angular/cdk                    13.3.6
@angular/cli                    13.2.6
@angular/flex-layout            13.0.0-beta.38
@angular/material               13.3.6
@schematics/angular             13.2.6
rxjs                            7.5.5
typescript                      4.5.5

Anything else?

I have seen runGuardsAndResolvers work in an older version of angular, but it was about a year and a half ago since I implemented that and it was for a different website.

@thavus
Copy link
Author

@thavus thavus commented May 15, 2022

I was able to do what I need with resolvers, but it seems extremally hacky to do it this way because a resolver is not a route guard. T_T

@thavus
Copy link
Author

@thavus thavus commented May 15, 2022

Ah and if it helps narrow this down. This is how my route looked before the resolver fix:

RouterModule.forChild([
      {
        path: '',
        pathMatch: 'full',
        component: StoryDetailsComponent,
        canDeactivate: [UnsavedDataGuard, RequiredDataGuard],
        runGuardsAndResolvers: ((from: ActivatedRouteSnapshot, to: ActivatedRouteSnapshot) => {
          console.log(from,to);

          return false;
        }),
        data: { animation: 'story-details' }
      }
    ])

UnsavedDataGuard returns a Promise<boolean> and RequiredDataGuard returns a boolean. All my routes are lazy loaded and they work fine as far as I can tell.

@thavus
Copy link
Author

@thavus thavus commented May 15, 2022

Found a better solution which let me use only the route guard. I read through the docs some more and found the nextState parameter.
image

This does exactly what I need and does not rely on resolvers and does not rely on runGuardsAndResolvers, which does not appear to be working. Hopefully this helps someone else who gets stuck on this. :)

Ignore the alert, I'll make something better later hehe. But the idea is, I don't want users navigating to any of the routes which are siblings to this one if they haven't filled out these required fields and submitted them

@atscott atscott added the comp: docs label May 16, 2022
@ngbot ngbot bot added this to the needsTriage milestone May 16, 2022
@atscott
Copy link
Contributor

@atscott atscott commented May 16, 2022

This is an issue with the documentation. RunGuardsAndResolvers only applies when the navigation doesn’t change the activated route. Probably would have been better named “rerunGuardsAndResolvers”.

That is, it applies only when they would otherwise not be run because the route isn’t being activated or deactivated.

@atscott atscott changed the title runGuardsAndResolvers, function not stepped into. runGuardsAndResolvers documentation doesn’t clearly describe when it’s used May 16, 2022
@atscott atscott added the P4 label May 16, 2022
@ngbot ngbot bot removed this from the needsTriage milestone May 16, 2022
@ngbot ngbot bot added this to the Backlog milestone May 16, 2022
@atscott atscott added the good first issue label May 16, 2022
@najmiehsa
Copy link

@najmiehsa najmiehsa commented May 29, 2022

@atscott Hi, the documentation for RunGuardsAndResolvers is archived and read only. How can I edit that?

@atscott
Copy link
Contributor

@atscott atscott commented May 31, 2022

@najmiehsa Documentation is generated from the JSDoc comments here and here

@najmiehsa
Copy link

@najmiehsa najmiehsa commented May 31, 2022

@atscott Thank you.

@yndue736

This comment was marked as spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp: docs comp: router good first issue P4
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants