angular/aio/content/guide
Andrew Scott de058bba99 feat(router): Add CanMatch guard to control whether a Route should match (#46021)
Currently we have two main types of guards:
`CanLoad`: decides if we can load a module (used with lazy loading)
`CanActivate` and friends. It decides if we can activate/deactivate a route.
So we always decide where we want to navigate first ("recognize") and create a new router state snapshot. And only then we run guards to check if the navigation should be allowed.
This doesn't handle one very important use case where we want to decide where to navigate based on some data (e.g., who the user is).
I suggest to add a new guard that allows us to do that.

```
[
  {path: 'home', component: AdminHomePage, canUse: [IsAdmin]},
  {path: 'home', component: SimpleHomePage}
]
```

Here, navigating to '/home' will render `AdminHomePage` if the user is an admin and will render 'SimpleHomePage' otherwise. Note that the url will remain '/home'.

With the introduction of standalone components and new features in the Router such as `loadComponent`,
there's a case for deprecating `CanLoad` and replacing it with the `CanMatch` guard. There are a few reasons for this:

* One of the intentions of having separate providers on a Route is that lazy
loading should not be an architectural feature of an application. It's an
optimization you do for code size. That is, there should not be an architectural
feature in the router to specifically control whether to lazy load something or
not based on conditions such as authentication. This is a slight nuanced
difference between the proposed canUse guard: this guard would control whether
you can use the route at all and as a side-effect, whether we download the code.
`CanLoad` only specified whether the code should be downloaded so canUse is more powerful and more appropriate.
* The naming of `CanLoad` will be potentially misunderstood for the `loadComponent` feature.
Because it applies to `loadChildren`, it feels reasonable to think that it will
also apply to `loadComponent`. This isn’t the case: since we don't need
to load the component until right before activation, we defer the
loading until all guards/resolvers have run.

When considering the removal of `CanLoad` and replacing it with `CanMatch`, this
does inform another decision that needed to be made: whether it makes sense for
`CanMatch` guards to return a UrlTree or if they should be restricted to just boolean.
The original thought was that no, these new guards should not allow returning UrlTree
because that significantly expands the intent of the feature from simply
“can I use the route” to “can I use this route, and if not, should I redirect?”
I now believe it should allowed to return `UrlTree` for several reasons:

* For feature parity with `CanLoad`
* Because whether we allow it as a return value or not, developers will still be
able to trigger a redirect from the guards using the `Router.navigate` function.
* Inevitably, there will be developers who disagree with the philosophical decision
to disallow `UrlTree` and we don’t necessarily have a compelling reason to refuse this as a feature.

Relates to #16211 - `CanMatch` instead of `CanActivate` would prevent
blank screen. Additional work is required to close this issue. This can
be accomplished by making the initial navigation result trackable (including
the redirects).
Resolves #14515
Replaces #16416
Resolves #34231
Resolves #17145
Resolves #12088

PR Close #46021
2022-06-13 22:53:49 +00:00
..
accessibility.md feat(router): add ariaCurrentWhenActive input to RouterLinkActive directive (#45167) 2022-04-20 15:14:01 -07:00
ajs-quick-reference.md docs: update links and TOC (#45897) 2022-05-16 16:07:34 -07:00
angular-compiler-options.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
angular-package-format.md docs: update angular package format guide to reflect recent changes (#45725) 2022-05-04 12:44:28 -07:00
animations.md docs: Fix several typos in the angular documentation (#45573) 2022-04-11 17:41:59 +00:00
aot-compiler.md docs: fix … presented in aio code docs (#46123) 2022-05-25 11:30:30 -07:00
aot-metadata-errors.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
app-shell.md docs: apply suggestions from code review (#45325) 2022-04-08 19:36:30 +00:00
architecture-components.md docs: fix template example (#46170) 2022-05-31 11:59:47 -07:00
architecture-modules.md docs: apply suggestions from code review (#45325) 2022-04-08 19:36:30 +00:00
architecture-next-steps.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
architecture-services.md docs: apply suggestions from code review (#45325) 2022-04-08 19:36:30 +00:00
architecture.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
attribute-binding.md docs: update links and TOC (#45897) 2022-05-16 16:07:34 -07:00
attribute-directives.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
binding-overview.md docs: new binding overview doc (#45897) 2022-05-16 16:07:34 -07:00
binding-syntax.md docs: apply suggestions from code review (#45325) 2022-04-08 19:36:30 +00:00
bootstrapping.md docs: apply suggestions from code review (#45325) 2022-04-08 19:36:30 +00:00
browser-support.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
build.md docs: rename stage to staging (#46164) 2022-05-31 11:59:18 -07:00
built-in-directives.md docs: make structural directives guide generic (#44895) 2022-05-23 13:44:41 -07:00
change-detection-skipping-subtrees.md docs: use better wording (#46314) 2022-06-10 14:21:06 +00:00
change-detection-slow-computations.md docs: add a guide on optimizing slow computations (#45880) 2022-05-09 13:56:34 -07:00
change-detection-zone-pollution.md docs: corrected the misspelled word (#46126) 2022-05-24 10:47:46 -07:00
change-detection.md docs: add general change detection overview (#45880) 2022-05-09 13:56:34 -07:00
cheatsheet.md docs: fix … presented in aio code docs (#46123) 2022-05-25 11:30:30 -07:00
class-binding.md docs: class-binding created as a separate task-based doc (#45897) 2022-05-16 16:07:33 -07:00
cli-builder.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
comparing-observables.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
complex-animation-sequences.md docs: replace double dashes with m-dashes, fix typos (#46174) 2022-06-01 11:02:41 -07:00
component-interaction.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
component-overview.md Fix typo (#45680) 2022-04-19 09:26:12 -07:00
component-styles.md docs: Remove outdated note (#46022) 2022-05-23 09:53:40 -07:00
content-projection.md docs: modify header and path (#45828) 2022-06-10 16:11:09 +00:00
contributors-guide-overview.md build: clean up references to old master branch (#45856) 2022-05-04 16:23:33 -07:00
creating-libraries.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
dependency-injection-in-action.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
dependency-injection-navtree.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
dependency-injection-providers.md docs: apply suggestions from code review (#45325) 2022-04-08 19:36:30 +00:00
dependency-injection.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
deployment.md docs: remove azure builder reference (#46285) 2022-06-08 12:40:23 -07:00
deprecations.md docs: fix … presented in aio code docs (#46123) 2022-05-25 11:30:30 -07:00
developer-guide-overview.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
devtools.md docs(devtools): add firefox extension references (#45985) 2022-05-16 18:04:51 +00:00
docs-style-guide.md build: clean up references to old master branch (#45856) 2022-05-04 16:23:33 -07:00
dynamic-component-loader.md docs: apply suggestions from code review (#45325) 2022-04-08 19:36:30 +00:00
dynamic-form.md docs: apply suggestions from code review (#45325) 2022-04-08 19:36:30 +00:00
elements.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
entry-components.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
event-binding-concepts.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
event-binding.md docs: updated Event binding doc (#45897) 2022-05-16 16:07:34 -07:00
example-apps-list.md refactor(docs-infra): Redirect old document title guide to new router feature (#45857) 2022-05-04 16:30:52 -07:00
feature-modules.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
file-structure.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
form-validation.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
forms-overview.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
forms.md docs: fix … presented in aio code docs (#46123) 2022-05-25 11:30:30 -07:00
frequent-ngmodules.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
glossary.md docs: fix "for to learn more" typo (#46095) 2022-06-10 14:24:37 +00:00
hierarchical-dependency-injection.md docs: change wrong alters to callouts (#46262) 2022-06-06 10:17:53 -07:00
http.md docs: fix missing closing div in http page (#46258) 2022-06-06 10:15:37 -07:00
i18n-common-add-package.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
i18n-common-deploy.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
i18n-common-format-data-locale.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
i18n-common-locale-id.md build: clean up references to old master branch (#45856) 2022-05-04 16:23:33 -07:00
i18n-common-merge.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
i18n-common-overview.md docs: update internationalization topics (#43722) 2021-10-12 21:38:37 +00:00
i18n-common-prepare.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
i18n-common-translation-files.md docs: fix … presented in aio code docs (#46123) 2022-05-25 11:30:30 -07:00
i18n-example.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
i18n-optional-import-global-variants.md docs(common): fix links to locales in i18n guides (#45661) 2022-04-19 09:17:33 -07:00
i18n-optional-manage-marked-text.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
i18n-optional-manual-runtime-locale.md docs(common): fix links to locales in i18n guides (#45661) 2022-04-19 09:17:33 -07:00
i18n-optional-overview.md docs: remove link (#43979) 2021-10-29 15:46:32 -07:00
i18n-overview.md docs: add i18n video to the overview page (#46273) 2022-06-06 11:46:49 -07:00
inputs-outputs.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
interpolation.md docs: update links and TOC (#45897) 2022-05-16 16:07:34 -07:00
language-service.md build: clean up references to old master branch (#45856) 2022-05-04 16:23:33 -07:00
lazy-loading-ngmodules.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
libraries.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
lifecycle-hooks.md docs: change wrong alert to callout (#46145) 2022-05-27 09:30:22 -07:00
lightweight-injection-tokens.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
localized-documentation.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
localizing-angular.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
migration-dynamic-flag.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
migration-injectable.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
migration-legacy-message-id.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
migration-localize.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
migration-module-with-providers.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
migration-renderer.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
migration-undecorated-classes.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
migration-update-libraries-tslib.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
module-types.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
ngmodule-api.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
ngmodule-faq.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
ngmodule-vs-jsmodule.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
ngmodules.md docs: fix angularfire2 hyperlink (#46046) 2022-05-23 13:52:11 -07:00
npm-packages.md docs: fix … presented in aio code docs (#46123) 2022-05-25 11:30:30 -07:00
observables-in-angular.md docs: update links and TOC (#45897) 2022-05-16 16:07:34 -07:00
observables.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
pipe-template.md docs: new Pipes in templates doc (#45897) 2022-05-16 16:07:34 -07:00
pipes-custom-data-trans.md docs: new Pipes custom data doc (#45505) 2022-04-12 21:29:06 +00:00
pipes-overview.md docs: Understanding pipes new doc (#45897) 2022-05-16 16:07:33 -07:00
pipes-transform-data.md docs: Pipes transform data new doc (#45897) 2022-05-16 16:07:33 -07:00
pipes.md docs: remove unnecesarry backslash from pipes documentation (#45916) 2022-05-09 12:08:03 -07:00
practical-observable-usage.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
prerendering.md docs: Fix several typos in the angular documentation (#45573) 2022-04-11 17:41:59 +00:00
property-binding-best-practices.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
property-binding.md docs: update links and TOC (#45897) 2022-05-16 16:07:34 -07:00
providers.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
reactive-forms.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
releases.md docs: mark version 13 as LTS in releases guide (#46268) 2022-06-06 11:48:05 -07:00
reusable-animations.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
reviewing-content.md build: clean up references to old master branch (#45856) 2022-05-04 16:23:33 -07:00
roadmap.md docs: fix incorrect title in the roadmap (#46140) 2022-05-26 17:42:24 -07:00
route-animations.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
router-reference.md feat(router): add ariaCurrentWhenActive input to RouterLinkActive directive (#45167) 2022-04-20 15:14:01 -07:00
router-tutorial-toh.md feat(router): Add CanMatch guard to control whether a Route should match (#46021) 2022-06-13 22:53:49 +00:00
router-tutorial.md feat(router): add ariaCurrentWhenActive input to RouterLinkActive directive (#45167) 2022-04-20 15:14:01 -07:00
router.md refactor(docs-infra): Redirect old document title guide to new router feature (#45857) 2022-05-04 16:30:52 -07:00
routing-overview.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
routing-with-urlmatcher.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
rx-library.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
schematics-authoring.md build: clean up references to old master branch (#45856) 2022-05-04 16:23:33 -07:00
schematics-for-libraries.md build: clean up references to old master branch (#45856) 2022-05-04 16:23:33 -07:00
schematics.md build: clean up references to old master branch (#45856) 2022-05-04 16:23:33 -07:00
security.md docs: add angular#bundler to Enforcing Trusted Types section (#45450) 2022-06-10 16:11:41 +00:00
service-worker-communications.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
service-worker-config.md docs: update service-worker-config to more clearly describe behavior (#45909) 2022-05-23 13:17:51 -07:00
service-worker-devops.md docs: replace broken <br/> from service worker (#46193) 2022-05-31 12:02:35 -07:00
service-worker-getting-started.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
service-worker-intro.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
service-worker-notifications.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
setup-local.md fix(docs-infra): correctly style the CLI version in code snippet (#46020) 2022-05-17 18:28:30 +00:00
sharing-ngmodules.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
singleton-services.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
standalone-components.md docs: add guide to standalone components preview (#46058) 2022-05-23 09:15:06 -07:00
static-query-migration.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
strict-mode.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
structural-directives.md docs: replace wrong NgFor with NgForOf (#46228) 2022-06-03 10:08:15 -07:00
style-precedence.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
styleguide.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
svg-in-templates.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
template-expression-operators.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
template-overview.md docs: add template overview doc (#45897) 2022-05-16 16:07:34 -07:00
template-reference-variables.md docs: Understanding template variables updated (#45897) 2022-05-16 16:07:33 -07:00
template-statements.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
template-syntax.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
template-typecheck.md docs: Fix several typos in the angular documentation (#45573) 2022-04-11 17:41:59 +00:00
test-debugging.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
testing-attribute-directives.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
testing-code-coverage.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
testing-components-basics.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
testing-components-scenarios.md docs: fix &hellip; presented in aio code docs (#46123) 2022-05-25 11:30:30 -07:00
testing-pipes.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
testing-services.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
testing-utility-apis.md docs(docs-infra): update createComponent description to fix incorrect method name and invalid use of html escaping (#46074) 2022-05-23 13:21:45 -07:00
testing.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
transition-and-triggers.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
two-way-binding.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
typed-forms.md docs: fix FormRecord example (#46124) 2022-05-24 10:47:07 -07:00
typescript-configuration.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
understanding-angular-overview.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
understanding-template-expr-overview.md docs: new Template expressions overview doc (#45897) 2022-05-16 16:07:33 -07:00
universal.md docs: Fix several typos in the angular documentation (#45573) 2022-04-11 17:41:59 +00:00
update-to-latest-version.md docs: update release and update guides for v14 (#46200) 2022-05-31 15:17:21 -07:00
updating-content-github-ui.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
updating-search-keywords.md build: clean up references to old master branch (#45856) 2022-05-04 16:23:33 -07:00
updating.md build: clean up references to old master branch (#45856) 2022-05-04 16:23:33 -07:00
upgrade-performance.md docs: change wrong alters to callouts (#46262) 2022-06-06 10:17:53 -07:00
upgrade-setup.md docs: Fix several typos in the angular documentation (#45573) 2022-04-11 17:41:59 +00:00
upgrade.md docs: change wrong alters to callouts (#46262) 2022-06-06 10:17:53 -07:00
user-input.md docs: change wrong alters to callouts (#46262) 2022-06-06 10:17:53 -07:00
using-libraries.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
view-encapsulation.md docs(core): fix typo in View Encapsulation code example (#45629) 2022-04-14 15:00:04 -07:00
web-worker.md docs: improve markdown (#45325) 2022-04-08 19:36:30 +00:00
what-is-angular.md docs: replace double dashes with m-dashes, fix typos (#46174) 2022-06-01 11:02:41 -07:00
workspace-config.md build: clean up references to old master branch (#45856) 2022-05-04 16:23:33 -07:00
zone.md docs: change wrong alters to callouts (#46262) 2022-06-06 10:17:53 -07:00