Skip to content

Commit 80d471d

Browse files
committed
Updated functional guard support
1 parent 7ca4617 commit 80d471d

File tree

6 files changed

+52
-54
lines changed

6 files changed

+52
-54
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"titleBar.activeBackground": "#1975d2",
2424
"titleBar.activeForeground": "#e7e7e7",
2525
"titleBar.inactiveBackground": "#1975d299",
26-
"titleBar.inactiveForeground": "#e7e7e799"
26+
"titleBar.inactiveForeground": "#e7e7e799",
27+
"commandCenter.border": "#e7e7e799"
2728
},
2829
"peacock.color": "1975d2"
2930
}

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## Angular Snippets Changelog
22

3+
<a name="16.0.0"></a>
4+
5+
# 16.0.0 (2023-06-15)
6+
7+
- Updated functional guard support for a-guard-can-activate, a-guard-can-activate-child, a-guard-can-match, and a-guard-can-deactivate
8+
39
<a name="13.0.0"></a>
410

511
# 13.0.0 (2022-01-31)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Angular TypeScript Snippets for VS Code
22

3-
**Updated for Angular 13.0.0 release**
3+
**Updated for Angular 16.0.0 release**
44

55
This extension for Visual Studio Code adds snippets for Angular for TypeScript and HTML.
66

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"publisher": "johnpapa",
44
"displayName": "Angular Snippets (Version 13)",
55
"description": "Angular version 13 snippets by John Papa",
6-
"version": "13.0.0",
6+
"version": "16.0.0",
77
"icon": "images/angular-shield.png",
88
"galleryBanner": {
99
"color": "#0273D4",

snippets/typescript.json

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,7 @@
246246
"Route definitions": {
247247
"prefix": "a-routes",
248248
"description": "Route definitions",
249-
"body": [
250-
"import { Routes } from '@angular/router';",
251-
"",
252-
"export const routes: Routes = [${0}]"
253-
]
249+
"body": ["import { Routes } from '@angular/router';", "", "export const routes: Routes = [${0}]"]
254250
},
255251
"Angular Module": {
256252
"prefix": "a-module",
@@ -286,80 +282,75 @@
286282
"prefix": "a-guard-can-activate",
287283
"description": "Angular CanActivate guard",
288284
"body": [
289-
"import { Injectable } from '@angular/core';",
290-
"import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot } from '@angular/router';",
285+
"import { inject } from '@angular/core';",
286+
"import { ActivatedRouteSnapshot, CanActivateFn, RouterStateSnapshot } from '@angular/router';",
291287
"",
292-
"@Injectable({providedIn: ${1:'root'}})",
293-
"export class ${2:Name}Guard implements CanActivate {",
294-
"\tconstructor() { }",
288+
"export const ${2:Name}Guard: CanActivateFn = (",
289+
"\troute: ActivatedRouteSnapshot,",
290+
"\tstate: RouterStateSnapshot",
291+
"\t) => {",
295292
"",
296-
"\tcanActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {",
297293
"\t\treturn true;$0",
298-
"\t}",
299294
"}"
300295
]
301296
},
302297
"Angular CanActivateChild Guard": {
303298
"prefix": "a-guard-can-activate-child",
304299
"description": "Angular CanActivateChild guard",
305300
"body": [
306-
"import { Injectable } from '@angular/core';",
307-
"import { ActivatedRouteSnapshot, CanActivateChild, RouterStateSnapshot } from '@angular/router';",
301+
"import { inject } from '@angular/core';",
302+
"import { ActivatedRouteSnapshot, CanActivateChildFn, RouterStateSnapshot } from '@angular/router';",
308303
"",
309-
"@Injectable({providedIn: ${1:'root'}})",
310-
"export class ${2:Name}Guard implements CanActivateChild {",
311-
"\tconstructor() { }",
304+
"export const ${2:Name}Guard: CanActivateChildFn = (",
305+
"\troute: ActivatedRouteSnapshot,",
306+
"\tstate: RouterStateSnapshot",
307+
"\t) => {",
312308
"",
313-
"\tcanActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {",
314309
"\t\treturn true;$0",
315-
"\t}",
316310
"}"
317311
]
318312
},
319-
"Angular CanLoad Guard": {
320-
"prefix": "a-guard-can-load",
321-
"description": "Angular CanLoad guard",
313+
"Angular CanMatch Guard": {
314+
"prefix": "a-guard-can-match",
315+
"description": "Angular CanMatch guard",
322316
"body": [
323-
"import { Injectable } from '@angular/core';",
324-
"import { CanLoad, Route } from '@angular/router';",
317+
"import { inject } from '@angular/core';",
318+
"import { CanMatchFn, Route, Router, UrlSegment } from '@angular/router';",
325319
"",
326-
"@Injectable({providedIn: ${1:'root'}})",
327-
"export class ${2:Name}Guard implements CanLoad {",
328-
"\tconstructor() { }",
329-
"",
330-
"\tcanLoad(route: Route) {",
331-
"\t\treturn true;$0",
332-
"\t}",
320+
"export const ${2:Name}Guard: CanMatchFn = (",
321+
"\troute: Route,",
322+
"\tsegments: UrlSegment[]",
323+
") => {",
324+
"\treturn true;$0",
333325
"}"
334326
]
335327
},
336328
"Angular CanDeactivate Guard": {
337329
"prefix": "a-guard-can-deactivate",
338330
"description": "Angular CanDeactivate guard",
339331
"body": [
340-
"import { Injectable } from '@angular/core';",
341-
"import { ActivatedRouteSnapshot, CanDeactivate, RouterStateSnapshot } from '@angular/router';",
332+
"import { inject } from '@angular/core';",
333+
"import { CanDeactivateFn } from '@angular/router';",
342334
"import { Observable } from 'rxjs';",
343335
"",
344-
"import { ${2:ComponentName}Component } from './${3:filename}.component';",
345-
"",
346336
"// Consider using this interface for all CanDeactivate guards,",
347337
"// and have your components implement this interface, too.",
348338
"//",
349-
"// e.g. export class CanDeactivateGuard implements CanDeactivate<CanComponentDeactivate> {",
339+
"// e.g. export class VillainsComponent implements CanComponentDeactivate { ...",
350340
"//",
351-
"// export interface CanComponentDeactivate {",
352-
"// canDeactivate: () => any;",
353-
"// }",
354-
"",
355-
"@Injectable({providedIn: ${4:'root'}})",
356-
"export class ${1:Name}Guard implements CanDeactivate<${2:ComponentName}Component> {",
357-
"\tcanDeactivate(",
358-
"\t\tcomponent: ${2:ComponentName}Component,",
359-
"\t\tcurrentRoute: ActivatedRouteSnapshot, ",
360-
"\t\tcurrentState: RouterStateSnapshot",
361-
"\t): Observable<boolean>|Promise<boolean>|boolean {",
362-
"\t\treturn false;$0",
341+
"export interface CanComponentDeactivate {",
342+
"\tcanDeactivate: () => Observable<boolean> | Promise<boolean> | boolean;",
343+
"}",
344+
"",
345+
"export const ${1:Name}Guard: CanDeactivateFn<CanComponentDeactivate> = (",
346+
"\tcomponent: CanComponentDeactivate",
347+
") => {",
348+
"\t\tif (component.canDeactivate()) {",
349+
"\t\t\tconsole.log(`💂‍♀️ [Guard] - Can Deactivate Guard - allowed`);",
350+
"\t\t\treturn true;",
351+
"\t\t} else {",
352+
"\t\t\tconsole.log(`💂‍♀️ [Guard] - Can Deactivate Guard - not allowed`);",
353+
"\t\t\treturn false;",
363354
"\t}",
364355
"}"
365356
]
@@ -380,7 +371,7 @@
380371
]
381372
},
382373
"Angular Network-Aware Preload Strategy": {
383-
"prefix": "a-preload-network-strategy",
374+
"prefix": "a-preload-network-strategy",
384375
"description": "Angular network aware preload strategy",
385376
"body": [
386377
"import { Injectable } from '@angular/core';",

0 commit comments

Comments
 (0)