René's URL Explorer Experiment


Title: feat(router): allow guards and resolvers to be plain functions by atscott · Pull Request #46684 · angular/angular · GitHub

Open Graph Title: feat(router): allow guards and resolvers to be plain functions by atscott · Pull Request #46684 · angular/angular

X Title: feat(router): allow guards and resolvers to be plain functions by atscott · Pull Request #46684 · angular/angular

Description: The current Router APIs require guards/resolvers to be present in the DI tree. This is because we want to treat all guards/resolvers equally and some may require dependencies. This requirement results in quite a lot of boilerplate for guards. Here are two examples: const MY_GUARD = new InjectionToken('my_guard'); … providers: {provide: MY_GUARD, useValue: () => window.someGlobalState} … const route = {path: 'somePath', canActivate: [MY_GUARD]} @Injectable({providedIn: 'root'}) export class MyGuardWithDependency { constructor(private myDep: MyDependency) {} canActivate() { return myDep.canActivate(); } } … const route = {path: 'somePath', canActivate: [MyGuardWithDependency]} Notice that even when we want to write a simple guard that has no dependencies as in the first example, we still have to write either an InjectionToken or an Injectable class. With this commit router guards and resolvers can be plain old functions. For example: const route = {path: 'somePath', component: EditCmp, canDeactivate: [(component: EditCmp) => !component.hasUnsavedChanges]} Additionally, these functions can still use Angular DI with inject from @angular/core. const route = {path: 'somePath', canActivate: [() => inject(MyDependency).canActivate()]} Reviewer notes: We should deprecate the Token and class-based guards/resolvers in a follow up. Doing so here would make landing the feature change more difficult. With the above, documentation updates were kept minimal. I have updated the API docs on the Route to indicate pure functions can be used instead of the previous token documentation (which is by far less common and cumbersome). Additionally, the Tour of Heroes only uses the class-based Injectable guards. These examples have also been kept unchanged since we haven't that deprecation will come later. Until then, the most common use-case is what's documented in the Tour of Heroes. A migration can be written along with or as a follow up to the deprecation. The size increased, but is actually pretty small considering the scope of the feature. In the long run, this will go back down when we no longer need to handle the deprecated/removed ways of doing guards/resolvers.

Open Graph Description: The current Router APIs require guards/resolvers to be present in the DI tree. This is because we want to treat all guards/resolvers equally and some may require dependencies. This requirement resu...

X Description: The current Router APIs require guards/resolvers to be present in the DI tree. This is because we want to treat all guards/resolvers equally and some may require dependencies. This requirement resu...

Opengraph URL: https://github.com/angular/angular/pull/46684

X: @github

direct link

Domain: github.com

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:a0f6df14-21c2-ffb0-a6c8-6a2f721a87af
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idB39C:2D40DE:10B2F6:154894:6B00F852
html-safe-nonce9c8b389640119096cedd8e19e1869ead9649ca8af1c8623a53b22d7e2dfc43ca
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMzlDOjJENDBERToxMEIyRjY6MTU0ODk0OjZCMDBGODUyIiwidmlzaXRvcl9pZCI6IjQ1MTEyNDY0MzE4ODkxOTMwNDIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacd5613387ae80ddfc0a712a5f7016f127949c3fdda1a4a9a855cc250a40d60995
hovercard-subject-tagpull_request:985419014
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/angular/angular/pull/46684/files
twitter:imagehttps://avatars.githubusercontent.com/u/479713?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/479713?s=400&v=4
og:image:altThe current Router APIs require guards/resolvers to be present in the DI tree. This is because we want to treat all guards/resolvers equally and some may require dependencies. This requirement resu...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
Nonee208d86428ec00a57ade75058166297440e8128a01e5287c31a53dc042bbf187
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/angular/angular git https://github.com/angular/angular.git
octolytics-dimension-user_id139426
octolytics-dimension-user_loginangular
octolytics-dimension-repository_id24195339
octolytics-dimension-repository_nwoangular/angular
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id24195339
octolytics-dimension-repository_network_root_nwoangular/angular
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release7dafd017fb7809a839c443dba08622af38e63c09
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/angular/angular/pull/46684/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fangular%2Fangular%2Fpull%2F46684%2Ffiles
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fangular%2Fangular%2Fpull%2F46684%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=angular%2Fangular
Reloadhttps://github.com/angular/angular/pull/46684/files
Reloadhttps://github.com/angular/angular/pull/46684/files
Reloadhttps://github.com/angular/angular/pull/46684/files
angularhttps://github.com/angular
angularhttps://github.com/angular/angular
Codehttps://github.com/angular/angular
Issues835 (835)https://github.com/angular/angular/issues
Pull requests141 (141)https://github.com/angular/angular/pulls
Discussionshttps://github.com/angular/angular/discussions
Actionshttps://github.com/angular/angular/actions
Projectshttps://github.com/angular/angular/projects
Security and quality40 (40)https://github.com/angular/angular/security
Insightshttps://github.com/angular/angular/insights
Sign up for GitHub https://github.com/signup?return_to=%2Fangular%2Fangular%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fangular%2Fangular%2Fissues%2Fnew%2Fchoose
atscotthttps://github.com/atscott
angular:mainhttps://github.com/angular/angular/tree/main
atscott:guardsAsFunctionshttps://github.com/atscott/angular/tree/guardsAsFunctions
Conversation 26 https://github.com/angular/angular/pull/46684
Commits 3 https://github.com/angular/angular/pull/46684/commits
Checks 0 https://github.com/angular/angular/pull/46684/checks
Files changed https://github.com/angular/angular/pull/46684/files
Please reload this pagehttps://github.com/angular/angular/pull/46684/files
feat(router): allow guards and resolvers to be plain functions https://github.com/angular/angular/pull/46684/files#top
Show all changes 3 commits https://github.com/angular/angular/pull/46684/files
6901145 feat(router): allow guards and resolvers to be plain functions atscott Jul 1, 2022 https://github.com/angular/angular/pull/46684/commits/6901145c74f62dc820924455f3dbaa586a4736fd
1077dc7 fixup! feat(router): allow guards and resolvers to be plain functions atscott Aug 5, 2022 https://github.com/angular/angular/pull/46684/commits/1077dc7362493842f6297a5951bb75220004db75
7125561 fixup! feat(router): allow guards and resolvers to be plain functions atscott Aug 5, 2022 https://github.com/angular/angular/pull/46684/commits/71255612aba45f338e5864118a2367b34f3e8248
Clear filters https://github.com/angular/angular/pull/46684/files
Please reload this pagehttps://github.com/angular/angular/pull/46684/files
Please reload this pagehttps://github.com/angular/angular/pull/46684/files
index.md https://github.com/angular/angular/pull/46684/files#diff-b66b8d89f0c8effbe51128387af21d4705a974d132a64ac6c73090cc73ff2905
integration-payloads.json https://github.com/angular/angular/pull/46684/files#diff-17036a5c93c1cb84bf295788071edcc333ed9961cfab07ca1db46f555d49c32f
application_ref.ts https://github.com/angular/angular/pull/46684/files#diff-c3bc2f4d2923398d46897c1233ab19cec2611e44ea60592e7e0d5ed03747a897
core_render3_private_export.ts https://github.com/angular/angular/pull/46684/files#diff-de09a25a93c0410c75a7ff56fdc6fb572198bdbce5ecaa98cb001063018eb79e
defs.ts https://github.com/angular/angular/pull/46684/files#diff-9fa456a4f61f0043f027bbe039f46dbc951f90c68627a9553020db5cc17cbf11
definition.ts https://github.com/angular/angular/pull/46684/files#diff-4374dd238deae3e4714315fc97bb9983092ada87475d8e0b8d28e191571941de
module.ts https://github.com/angular/angular/pull/46684/files#diff-d7312b298dbbe322603ff75633c99e8074bc20ed5d4370392ec2b80d86c3bf62
bundle.golden_symbols.json https://github.com/angular/angular/pull/46684/files#diff-0ea07045c567fb07c48994161c0a3bfa5bd1a676bc484d0fd8d3f9c5c2bc5d8d
index.ts https://github.com/angular/angular/pull/46684/files#diff-60f7c2fcb5faf15b8cf44d1cadc6c78e266a83e6addf94bdf3f4b5fee82549a1
models.ts https://github.com/angular/angular/pull/46684/files#diff-b8e9749ad4562cf0925fd464daee97ba96d82e28fd9f7b45bd1bf0e0342fa814
check_guards.ts https://github.com/angular/angular/pull/46684/files#diff-1706bbbdd4dda445171e80023d67b54f1aa77a457d6b81b6815239113fd34dec
resolve_data.ts https://github.com/angular/angular/pull/46684/files#diff-fcb2cfa4f8768f15aa076c8e7fa930b9c9173883badc599053673675a5a0078d
preactivation.ts https://github.com/angular/angular/pull/46684/files#diff-31e1de19f2a52e002a10281344216addd2810114083bb01f3c8497bed828b723
apply_redirects.spec.ts https://github.com/angular/angular/pull/46684/files#diff-f2a6ddca34dd8b18717187c7b81638613c564d9aa1fc72e99434fc66a78e4ba5
integration.spec.ts https://github.com/angular/angular/pull/46684/files#diff-c89e4eb845a28c0d2385a4460f9d27259558686f192bdb0fe61953e057be803e
resolve_data.spec.ts https://github.com/angular/angular/pull/46684/files#diff-9f971938cd3e5642b7d77f1243a1797ad21fc6655e1acfe747e7e175b3a1d276
page_title_strategy_spec.ts https://github.com/angular/angular/pull/46684/files#diff-6cd3e828618fd4173feb0cd8667d65097460225f6b162d2280edcb99e97f1f55
router.spec.ts https://github.com/angular/angular/pull/46684/files#diff-d6c3501982a955764313ebf0d479f8d466379cafc9fff33b7c48fe4cd901b3ed
goldens/public-api/router/index.mdhttps://github.com/angular/angular/pull/46684/files#diff-b66b8d89f0c8effbe51128387af21d4705a974d132a64ac6c73090cc73ff2905
View file https://github.com/atscott/angular/blob/71255612aba45f338e5864118a2367b34f3e8248/goldens/public-api/router/index.md
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/angular/angular/pull/46684/{{ revealButtonHref }}
https://github.com/angular/angular/pull/46684/files#diff-b66b8d89f0c8effbe51128387af21d4705a974d132a64ac6c73090cc73ff2905
https://github.com/angular/angular/pull/46684/files#diff-b66b8d89f0c8effbe51128387af21d4705a974d132a64ac6c73090cc73ff2905
https://github.com/angular/angular/pull/46684/files#diff-b66b8d89f0c8effbe51128387af21d4705a974d132a64ac6c73090cc73ff2905
https://github.com/angular/angular/pull/46684/files#diff-b66b8d89f0c8effbe51128387af21d4705a974d132a64ac6c73090cc73ff2905
https://github.com/angular/angular/pull/46684/files#diff-b66b8d89f0c8effbe51128387af21d4705a974d132a64ac6c73090cc73ff2905
Please reload this pagehttps://github.com/angular/angular/pull/46684/files
Please reload this pagehttps://github.com/angular/angular/pull/46684/files
https://github.com/angular/angular/pull/46684/files#diff-b66b8d89f0c8effbe51128387af21d4705a974d132a64ac6c73090cc73ff2905
https://github.com/angular/angular/pull/46684/files#diff-b66b8d89f0c8effbe51128387af21d4705a974d132a64ac6c73090cc73ff2905
goldens/size-tracking/integration-payloads.jsonhttps://github.com/angular/angular/pull/46684/files#diff-17036a5c93c1cb84bf295788071edcc333ed9961cfab07ca1db46f555d49c32f
View file https://github.com/atscott/angular/blob/71255612aba45f338e5864118a2367b34f3e8248/goldens/size-tracking/integration-payloads.json
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/angular/angular/pull/46684/{{ revealButtonHref }}
https://github.com/angular/angular/pull/46684/files#diff-17036a5c93c1cb84bf295788071edcc333ed9961cfab07ca1db46f555d49c32f
https://github.com/angular/angular/pull/46684/files#diff-17036a5c93c1cb84bf295788071edcc333ed9961cfab07ca1db46f555d49c32f
packages/core/src/application_ref.tshttps://github.com/angular/angular/pull/46684/files#diff-c3bc2f4d2923398d46897c1233ab19cec2611e44ea60592e7e0d5ed03747a897
View file https://github.com/atscott/angular/blob/71255612aba45f338e5864118a2367b34f3e8248/packages/core/src/application_ref.ts
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/angular/angular/pull/46684/{{ revealButtonHref }}
https://github.com/angular/angular/pull/46684/files#diff-c3bc2f4d2923398d46897c1233ab19cec2611e44ea60592e7e0d5ed03747a897
https://github.com/angular/angular/pull/46684/files#diff-c3bc2f4d2923398d46897c1233ab19cec2611e44ea60592e7e0d5ed03747a897
packages/core/src/core_render3_private_export.tshttps://github.com/angular/angular/pull/46684/files#diff-de09a25a93c0410c75a7ff56fdc6fb572198bdbce5ecaa98cb001063018eb79e
View file https://github.com/atscott/angular/blob/71255612aba45f338e5864118a2367b34f3e8248/packages/core/src/core_render3_private_export.ts
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/angular/angular/pull/46684/{{ revealButtonHref }}
https://github.com/angular/angular/pull/46684/files#diff-de09a25a93c0410c75a7ff56fdc6fb572198bdbce5ecaa98cb001063018eb79e
https://github.com/angular/angular/pull/46684/files#diff-de09a25a93c0410c75a7ff56fdc6fb572198bdbce5ecaa98cb001063018eb79e
https://github.com/angular/angular/pull/46684/files#diff-de09a25a93c0410c75a7ff56fdc6fb572198bdbce5ecaa98cb001063018eb79e
https://github.com/angular/angular/pull/46684/files#diff-de09a25a93c0410c75a7ff56fdc6fb572198bdbce5ecaa98cb001063018eb79e
packages/core/src/di/interface/defs.tshttps://github.com/angular/angular/pull/46684/files#diff-9fa456a4f61f0043f027bbe039f46dbc951f90c68627a9553020db5cc17cbf11
View file https://github.com/atscott/angular/blob/71255612aba45f338e5864118a2367b34f3e8248/packages/core/src/di/interface/defs.ts
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/angular/angular/pull/46684/{{ revealButtonHref }}
https://github.com/angular/angular/pull/46684/files#diff-9fa456a4f61f0043f027bbe039f46dbc951f90c68627a9553020db5cc17cbf11
https://github.com/angular/angular/pull/46684/files#diff-9fa456a4f61f0043f027bbe039f46dbc951f90c68627a9553020db5cc17cbf11
packages/core/src/render3/definition.tshttps://github.com/angular/angular/pull/46684/files#diff-4374dd238deae3e4714315fc97bb9983092ada87475d8e0b8d28e191571941de
View file https://github.com/atscott/angular/blob/71255612aba45f338e5864118a2367b34f3e8248/packages/core/src/render3/definition.ts
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/angular/angular/pull/46684/{{ revealButtonHref }}
https://github.com/angular/angular/pull/46684/files#diff-4374dd238deae3e4714315fc97bb9983092ada87475d8e0b8d28e191571941de
https://github.com/angular/angular/pull/46684/files#diff-4374dd238deae3e4714315fc97bb9983092ada87475d8e0b8d28e191571941de
https://github.com/angular/angular/pull/46684/files#diff-4374dd238deae3e4714315fc97bb9983092ada87475d8e0b8d28e191571941de
https://github.com/angular/angular/pull/46684/files#diff-4374dd238deae3e4714315fc97bb9983092ada87475d8e0b8d28e191571941de
packages/core/src/render3/jit/module.tshttps://github.com/angular/angular/pull/46684/files#diff-d7312b298dbbe322603ff75633c99e8074bc20ed5d4370392ec2b80d86c3bf62
View file https://github.com/atscott/angular/blob/71255612aba45f338e5864118a2367b34f3e8248/packages/core/src/render3/jit/module.ts
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/angular/angular/pull/46684/{{ revealButtonHref }}
https://github.com/angular/angular/pull/46684/files#diff-d7312b298dbbe322603ff75633c99e8074bc20ed5d4370392ec2b80d86c3bf62
https://github.com/angular/angular/pull/46684/files#diff-d7312b298dbbe322603ff75633c99e8074bc20ed5d4370392ec2b80d86c3bf62
https://github.com/angular/angular/pull/46684/files#diff-d7312b298dbbe322603ff75633c99e8074bc20ed5d4370392ec2b80d86c3bf62
https://github.com/angular/angular/pull/46684/files#diff-d7312b298dbbe322603ff75633c99e8074bc20ed5d4370392ec2b80d86c3bf62
packages/core/test/bundling/router/bundle.golden_symbols.jsonhttps://github.com/angular/angular/pull/46684/files#diff-0ea07045c567fb07c48994161c0a3bfa5bd1a676bc484d0fd8d3f9c5c2bc5d8d
View file https://github.com/atscott/angular/blob/71255612aba45f338e5864118a2367b34f3e8248/packages/core/test/bundling/router/bundle.golden_symbols.json
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/angular/angular/pull/46684/{{ revealButtonHref }}
https://github.com/angular/angular/pull/46684/files#diff-0ea07045c567fb07c48994161c0a3bfa5bd1a676bc484d0fd8d3f9c5c2bc5d8d
https://github.com/angular/angular/pull/46684/files#diff-0ea07045c567fb07c48994161c0a3bfa5bd1a676bc484d0fd8d3f9c5c2bc5d8d
packages/router/src/index.tshttps://github.com/angular/angular/pull/46684/files#diff-60f7c2fcb5faf15b8cf44d1cadc6c78e266a83e6addf94bdf3f4b5fee82549a1
View file https://github.com/atscott/angular/blob/71255612aba45f338e5864118a2367b34f3e8248/packages/router/src/index.ts
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/angular/angular/pull/46684/{{ revealButtonHref }}
https://github.com/angular/angular/pull/46684/files#diff-60f7c2fcb5faf15b8cf44d1cadc6c78e266a83e6addf94bdf3f4b5fee82549a1
https://github.com/angular/angular/pull/46684/files#diff-60f7c2fcb5faf15b8cf44d1cadc6c78e266a83e6addf94bdf3f4b5fee82549a1
Please reload this pagehttps://github.com/angular/angular/pull/46684/files
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.