5 lines
49 KiB
JSON
5 lines
49 KiB
JSON
{
|
|
"id": "api/router/CanActivate",
|
|
"title": "CanActivate",
|
|
"contents": "\n\n<article>\n <div class=\"breadcrumb-container\">\n <div class=\"breadcrumb\">\n <script type=\"application/ld+json\">\n {\n \"@context\": \"http://schema.org\",\n \"@type\": \"BreadcrumbList\",\n \"itemListElement\": [\n { \"@type\": \"ListItem\", \"position\": 1, \"item\": { \"@id\": \"https://angular.io//api\", \"name\": \"API\" } },\n { \"@type\": \"ListItem\", \"position\": 2, \"item\": { \"@id\": \"https://angular.io/api/router\", \"name\": \"@angular/router\" } },\n { \"@type\": \"ListItem\", \"position\": 3, \"item\": { \"@id\": \"https://angular.io/api/router/CanActivate\", \"name\": \"CanActivate\" } }\n ]\n }\n </script>\n <a href=\"/api\">API</a> > <a href=\"api/router\">@angular/router</a>\n </div>\n <div class=\"github-links\">\n <a href=\"https://github.com/angular/angular/edit/master/packages/router/src/interfaces.ts?message=docs(router)%3A%20describe%20your%20change...#L14-L95\" aria-label=\"Suggest Edits\" title=\"Suggest Edits\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">mode_edit</i></a>\n <a href=\"https://github.com/angular/angular/tree/12.0.0-next.7/packages/router/src/interfaces.ts#L14-L95\" aria-label=\"View Source\" title=\"View Source\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">code</i></a>\n</div>\n </div>\n \n <header class=\"api-header\">\n <h1 id=\"canactivate\">CanActivate<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/router/CanActivate#canactivate\"><i class=\"material-icons\">link</i></a></h1>\n \n <label class=\"api-type-label interface\">interface</label>\n \n \n \n </header>\n \n <aio-toc class=\"embedded\"></aio-toc>\n\n <div class=\"api-body\">\n \n <section class=\"short-description\">\n <p>Interface that a class can implement to be a guard deciding if a route can be activated.\nIf all guards return <code>true</code>, navigation continues. If any guard returns <code>false</code>,\nnavigation is cancelled. If any guard returns a <code><a href=\"api/router/UrlTree\" class=\"code-anchor\">UrlTree</a></code>, the current navigation\nis cancelled and a new navigation begins to the <code><a href=\"api/router/UrlTree\" class=\"code-anchor\">UrlTree</a></code> returned from the guard.</p>\n\n <p><a href=\"api/router/CanActivate#description\">See more...</a></p>\n </section>\n \n \n <section class=\"interface-overview\">\n<code-example language=\"ts\" hidecopy=\"true\">\ninterface <a href=\"api/router/CanActivate\" class=\"code-anchor\">CanActivate</a> {\n <a class=\"code-anchor\" href=\"api/router/CanActivate#canActivate\"><span class=\"member-name\">canActivate</span>(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree</a>\n}\n</code-example>\n\n \n \n\n\n \n \n\n</section>\n\n \n\n \n \n<section class=\"description\">\n <h2 id=\"description\">Description<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/router/CanActivate#description\"><i class=\"material-icons\">link</i></a></h2>\n <p>The following example implements a <code><a href=\"api/router/CanActivate\" class=\"code-anchor\">CanActivate</a></code> function that checks whether the\ncurrent user has permission to activate the requested route.</p>\n<code-example>\nclass UserToken {}\nclass Permissions {\n canActivate(user: UserToken, id: string): boolean {\n return true;\n }\n}\n\n@<a href=\"api/core/Injectable\" class=\"code-anchor\">Injectable</a>()\nclass CanActivateTeam implements <a href=\"api/router/CanActivate\" class=\"code-anchor\">CanActivate</a> {\n constructor(private permissions: Permissions, private currentUser: UserToken) {}\n\n canActivate(\n route: <a href=\"api/router/ActivatedRouteSnapshot\" class=\"code-anchor\">ActivatedRouteSnapshot</a>,\n <a href=\"api/animations/state\" class=\"code-anchor\">state</a>: <a href=\"api/router/RouterStateSnapshot\" class=\"code-anchor\">RouterStateSnapshot</a>\n ): Observable<boolean|<a href=\"api/router/UrlTree\" class=\"code-anchor\">UrlTree</a>>|Promise<boolean|<a href=\"api/router/UrlTree\" class=\"code-anchor\">UrlTree</a>>|boolean|<a href=\"api/router/UrlTree\" class=\"code-anchor\">UrlTree</a> {\n return this.permissions.canActivate(this.currentUser, route.params.id);\n }\n}\n</code-example>\n<p>Here, the defined guard function is provided as part of the <code><a href=\"api/router/Route\" class=\"code-anchor\">Route</a></code> object\nin the router configuration:</p>\n<code-example>\n@<a href=\"api/core/NgModule\" class=\"code-anchor\">NgModule</a>({\n imports: [\n RouterModule.forRoot([\n {\n path: 'team/:id',\n component: TeamComponent,\n canActivate: [CanActivateTeam]\n }\n ])\n ],\n providers: [CanActivateTeam, UserToken, Permissions]\n})\nclass AppModule {}\n</code-example>\n<p>You can alternatively provide an in-line function with the <code>canActivate</code> signature:</p>\n<code-example>\n@<a href=\"api/core/NgModule\" class=\"code-anchor\">NgModule</a>({\n imports: [\n RouterModule.forRoot([\n {\n path: 'team/:id',\n component: TeamComponent,\n canActivate: ['canActivateTeam']\n }\n ])\n ],\n providers: [\n {\n provide: 'canActivateTeam',\n useValue: (route: <a href=\"api/router/ActivatedRouteSnapshot\" class=\"code-anchor\">ActivatedRouteSnapshot</a>, <a href=\"api/animations/state\" class=\"code-anchor\">state</a>: <a href=\"api/router/RouterStateSnapshot\" class=\"code-anchor\">RouterStateSnapshot</a>) => true\n }\n ]\n})\nclass AppModule {}\n</code-example>\n\n \n</section>\n\n \n\n \n\n<section class=\"instance-methods\">\n <h2 id=\"methods\">Methods<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/router/CanActivate#methods\"><i class=\"material-icons\">link</i></a></h2>\n \n <a id=\"canActivate\"></a>\n<table class=\"is-full-width method-table instance-method\">\n <thead><tr><th>\n <div class=\"with-github-links\">\n <h3 id=\"canactivate-1\">\n canActivate()\n \n <a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/router/CanActivate#canactivate-1\"><i class=\"material-icons\">link</i></a></h3>\n <div class=\"github-links\">\n <a href=\"https://github.com/angular/angular/edit/master/packages/router/src/interfaces.ts?message=docs(router)%3A%20describe%20your%20change...#L93-L94\" aria-label=\"Suggest Edits\" title=\"Suggest Edits\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">mode_edit</i></a>\n <a href=\"https://github.com/angular/angular/tree/12.0.0-next.7/packages/router/src/interfaces.ts#L93-L94\" aria-label=\"View Source\" title=\"View Source\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">code</i></a>\n</div>\n </div>\n </th></tr></thead>\n <tbody>\n \n \n <tr>\n <td>\n <div class=\"overload-info\">\n \n\n <code-example language=\"ts\" hidecopy=\"true\" class=\"no-box api-heading\"><span class=\"member-name\">canActivate</span>(route: <a href=\"api/router/ActivatedRouteSnapshot\" class=\"code-anchor\">ActivatedRouteSnapshot</a>, <a href=\"api/animations/state\" class=\"code-anchor\">state</a>: <a href=\"api/router/RouterStateSnapshot\" class=\"code-anchor\">RouterStateSnapshot</a>): Observable<boolean | <a href=\"api/router/UrlTree\" class=\"code-anchor\">UrlTree</a>> | Promise<boolean | <a href=\"api/router/UrlTree\" class=\"code-anchor\">UrlTree</a>> | boolean | <a href=\"api/router/UrlTree\" class=\"code-anchor\">UrlTree</a></code-example>\n\n \n\n <h6 class=\"no-anchor\" id=\"parameters\">Parameters</h6>\n <table class=\"is-full-width list-table parameters-table instance-method-overload-parameters\">\n <tbody>\n \n <tr class=\"instance-method-overload-parameter\">\n <td class=\"param-name\">\n <a id=\"\"></a>\n <code>route</code>\n </td>\n <td class=\"param-type\"><code><a href=\"api/router/ActivatedRouteSnapshot\" class=\"code-anchor\">ActivatedRouteSnapshot</a></code></td>\n <td class=\"param-description\">\n \n \n </td>\n </tr>\n <tr class=\"instance-method-overload-parameter\">\n <td class=\"param-name\">\n <a id=\"\"></a>\n <code><a href=\"api/animations/state\" class=\"code-anchor\">state</a></code>\n </td>\n <td class=\"param-type\"><code><a href=\"api/router/RouterStateSnapshot\" class=\"code-anchor\">RouterStateSnapshot</a></code></td>\n <td class=\"param-description\">\n \n \n </td>\n </tr>\n </tbody>\n</table>\n\n \n <h6 class=\"no-anchor\" id=\"returns\">Returns</h6>\n <p><code>Observable<boolean | <a href=\"api/router/UrlTree\" class=\"code-anchor\">UrlTree</a>> | Promise<boolean | <a href=\"api/router/UrlTree\" class=\"code-anchor\">UrlTree</a>> | boolean | <a href=\"api/router/UrlTree\" class=\"code-anchor\">UrlTree</a></code></p>\n\n \n\n\n \n\n \n</div>\n </td>\n </tr>\n \n\n \n\n \n </tbody>\n</table>\n\n \n</section>\n\n\n \n\n\n </div>\n</article>\n\n<!-- links to this doc:\n - api/router\n - api/router/Route\n - guide/cheatsheet\n - guide/router\n - guide/router-tutorial-toh\n-->\n<!-- links from this doc:\n - /api\n - api/animations/state\n - api/core/Injectable\n - api/core/NgModule\n - api/router\n - api/router/ActivatedRouteSnapshot\n - api/router/CanActivate#canActivate\n - api/router/CanActivate#canactivate\n - api/router/CanActivate#canactivate-1\n - api/router/CanActivate#description\n - api/router/CanActivate#methods\n - api/router/Route\n - api/router/RouterStateSnapshot\n - api/router/UrlTree\n - https://github.com/angular/angular/edit/master/packages/router/src/interfaces.ts?message=docs(router)%3A%20describe%20your%20change...#L14-L95\n - https://github.com/angular/angular/edit/master/packages/router/src/interfaces.ts?message=docs(router)%3A%20describe%20your%20change...#L93-L94\n - https://github.com/angular/angular/tree/12.0.0-next.7/packages/router/src/interfaces.ts#L14-L95\n - https://github.com/angular/angular/tree/12.0.0-next.7/packages/router/src/interfaces.ts#L93-L94\n-->"
|
|
} |