angular 路由守卫
<h3><span style="font-family: 黑体; font-size: 16px"><strong>一. canActive 使用场景:导航栏某个标签,如用户设置页面,只有登录才可以查看,不登录则点击无效</strong></span></h3><p><span style="font-family: 黑体; font-size: 16px"><strong>说明: canActive--是否能调用当前路由</strong></span></p>
<p><span style="font-family: 黑体; font-size: 16px"><strong>实现:</strong></span></p>
<p><span style="font-family: 黑体; font-size: 16px"><strong> 1. 创建路由守卫类 </strong></span></p>
<p><span style="font-family: 黑体; font-size: 16px"><strong> </strong></span></p>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, "Courier New", monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(152, 114, 162, 1)">import<span style="color: rgba(197, 200, 198, 1)"> { <span style="color: rgba(96, 137, 180, 1)">CanActivate<span style="color: rgba(197, 200, 198, 1)"> } <span style="color: rgba(152, 114, 162, 1)">from<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(154, 168, 58, 1)">'@angular/router'</span></span></span></span></span></span></span></div>
<div><span style="color: rgba(152, 114, 162, 1)">export<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(152, 114, 162, 1)">class<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(155, 0, 0, 1)">LoginGuard<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(152, 114, 162, 1)">implements<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(199, 68, 74, 1)">CanActivate<span style="color: rgba(197, 200, 198, 1)">{</span></span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)"> loginState<span style="color: rgba(103, 104, 103, 1)">:<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(199, 68, 74, 1)">boolean<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(103, 104, 103, 1)">=<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(64, 128, 128, 1)">false</span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(206, 103, 0, 1)">canActivate<span style="color: rgba(197, 200, 198, 1)">() {</span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(152, 114, 162, 1)">if<span style="color: rgba(197, 200, 198, 1)">(<span style="color: rgba(199, 68, 74, 1)">this<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(152, 114, 162, 1)">loginState<span style="color: rgba(197, 200, 198, 1)">) {</span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(152, 114, 162, 1)">return<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(64, 128, 128, 1)">true</span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)"> }<span style="color: rgba(152, 114, 162, 1)">else<span style="color: rgba(197, 200, 198, 1)"> {</span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(152, 114, 162, 1)">return<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(64, 128, 128, 1)">false</span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)"> }</span></div>
<div><span style="color: rgba(197, 200, 198, 1)"> }</span></div>
<div><span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(206, 103, 0, 1)">login<span style="color: rgba(197, 200, 198, 1)">() {</span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(199, 68, 74, 1)">this<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(152, 114, 162, 1)">loginState<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(103, 104, 103, 1)">=<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(64, 128, 128, 1)">true</span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)"> }</span></div>
<div><span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(206, 103, 0, 1)">loginOut<span style="color: rgba(197, 200, 198, 1)">() {</span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(199, 68, 74, 1)">this<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(152, 114, 162, 1)">loginState<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(103, 104, 103, 1)">=<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(64, 128, 128, 1)">false</span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)"> }</span></div>
<div><span style="color: rgba(197, 200, 198, 1)">}</span></div>
</div>
<p><span style="font-family: 黑体; font-size: 16px"><strong> 2. 在 app-routing.module.ts 或者在 app.modules.ts 中注入</strong></span></p>
<p><span style="font-family: 黑体; font-size: 16px"><strong> <code class="language-css"><span class="token atrule"><span class="token rule">@NgModule<span class="token punctuation">(<span class="token punctuation">{<br> <span class="token property">imports<span class="token punctuation">: <span class="token punctuation">,<br> <span class="token property">exports<span class="token punctuation">: <span class="token punctuation">,<br> <span class="token property">providers<span class="token punctuation">: <br><span class="token punctuation"> }<span class="token punctuation">)<br> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></strong></span></p>
<p><span style="font-family: 黑体; font-size: 16px"><strong><span class="token atrule"><span class="token rule"><span class="token punctuation"><span class="token punctuation"><span class="token property"><span class="token punctuation"><span class="token function"><span class="token punctuation"><span class="token punctuation"><span class="token punctuation"><span class="token property"><span class="token punctuation"><span class="token punctuation"><span class="token property"><span class="token punctuation"><span class="token punctuation"><span class="token punctuation"><span class="token punctuation"> 3. 在 app-routing.module.ts 中配置</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></strong></span></p>
<p><span style="font-family: 黑体; font-size: 16px"><strong><span class="token atrule"><span class="token rule"><span class="token punctuation"><span class="token punctuation"><span class="token property"><span class="token punctuation"><span class="token function"><span class="token punctuation"><span class="token punctuation"><span class="token punctuation"><span class="token property"><span class="token punctuation"><span class="token punctuation"><span class="token property"><span class="token punctuation"><span class="token punctuation"><span class="token punctuation"><span class="token punctuation"> import { LoginGuard } from './loginGuard/login.guard'<br> import { UnsaveGuard } from './loginGuard/unsave.guard'<br> const routes: Routes = [<br> { path: '', redirectTo: 'home', pathMatch: 'full' },<br> { path: 'app', component: AppComponent },<br> { path: 'directive', component: TemplateDirective },<br> { path: 'book', component: TemplateBookComponent, children: bookChildrenRoutes},<br> { path: 'details/:id', component: bookDetailsComponent },<br> { path: 'usersetting', component: UserSettingsComponent, canActivate: , canDeactivate: },<br> { path: 'home', component: HomeComponent},<br> ];</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></strong></span></p>
<p><span style="font-family: 黑体; font-size: 16px"><strong><span class="token atrule"><span class="token rule"><span class="token punctuation"><span class="token punctuation"><span class="token property"><span class="token punctuation"><span class="token function"><span class="token punctuation"><span class="token punctuation"><span class="token punctuation"><span class="token property"><span class="token punctuation"><span class="token punctuation"><span class="token property"><span class="token punctuation"><span class="token punctuation"><span class="token punctuation"><span class="token punctuation"> 4. 使用,在 home 页面有登录和退出的按钮,点击改变登录状态,查看路由的可访问性</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></strong></span></p>
<p><span style="font-family: 黑体; font-size: 16px"><strong><span class="token atrule"><span class="token rule"><span class="token punctuation"><span class="token punctuation"><span class="token property"><span class="token punctuation"><span class="token function"><span class="token punctuation"><span class="token punctuation"><span class="token punctuation"><span class="token property"><span class="token punctuation"><span class="token punctuation"><span class="token property"><span class="token punctuation"><span class="token punctuation"><span class="token punctuation"><span class="token punctuation"> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></strong></span></p>
<p> import { LoginGuard } from '../loginGuard/login.guard' </p>
<p> export class HomeComponent implements OnInit { </p>
<p> constructor( private loginGuard: LoginGuard ) { }</p>
<p> loginIn() { alert("登录") this.loginGuard.login() } </p>
<p> loginOut() { alert("退出") this.loginGuard.loginOut() } </p>
<p> ngOnInit() { } </p>
<p> }</p>
<p><span style="font-family: 黑体; font-size: 16px"><strong><span class="token atrule"><span class="token rule"><span class="token punctuation"><span class="token punctuation"><span class="token property"><span class="token punctuation"><span class="token function"><span class="token punctuation"><span class="token punctuation"><span class="token punctuation"><span class="token property"><span class="token punctuation"><span class="token punctuation"><span class="token property"><span class="token punctuation"><span class="token punctuation"><span class="token punctuation"><span class="token punctuation"> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></strong></span></p>
<p><span style="font-family: 黑体; font-size: 16px"><strong>二. CanDetivate 使用场景: 用户设置页面,如果没点击保存,提示用户,点击了保存直接离开</strong></span></p>
<p><span style="font-family: 黑体; font-size: 16px"><strong>说明: CanDetivate--是否可以离开路由</strong></span></p>
<p><span style="font-family: 黑体; font-size: 16px"><strong>实现:</strong></span></p>
<p><span style="font-family: 黑体; font-size: 16px"><strong> 1. 创建 canDetivate 类</strong></span></p>
<p><span style="font-family: 黑体; font-size: 16px"><strong> import { CanDeactivate } from '@angular/router'<br> import { UserSettingsComponent } from '../user-settings/user-settings.component'<br> // UserSettingsComponent--要守卫的组件类<br> export class UnsaveGuard implements CanDeactivate<UserSettingsComponent> {<br> saveState: boolean = false<br> /* return true 直接离开 return false 不能离开该路由 */<br> canDeactivate(component: UserSettingsComponent) {<br> if(this.saveState) {<br> return true<br> }else {<br> return window.confirm("您还没保存,确认离开吗?")<br> }<br> }<br> changeSaveState() {<br> this.saveState = true<br> }<br> }</strong></span></p>
<p> </p>
<p> </p>
<p> 2. 在 app-routing.module.ts 或者在 app.modules.ts 中注入</p>
<p><span style="font-family: 黑体; font-size: 16px"><strong> </strong></span></p>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, "Courier New", monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(154, 155, 153, 1)">/ 路由守卫</span></div>
<div><span style="color: rgba(152, 114, 162, 1)">import<span style="color: rgba(197, 200, 198, 1)"> { <span style="color: rgba(96, 137, 180, 1)">LoginGuard<span style="color: rgba(197, 200, 198, 1)"> } <span style="color: rgba(152, 114, 162, 1)">from<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(154, 168, 58, 1)">'./loginGuard/login.guard'</span></span></span></span></span></span></span></div>
<div><span style="color: rgba(152, 114, 162, 1)">import<span style="color: rgba(197, 200, 198, 1)"> { <span style="color: rgba(96, 137, 180, 1)">UnsaveGuard<span style="color: rgba(197, 200, 198, 1)"> } <span style="color: rgba(152, 114, 162, 1)">from<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(154, 168, 58, 1)">'./loginGuard/unsave.guard'</span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">@<span style="color: rgba(206, 103, 0, 1)">NgModule<span style="color: rgba(197, 200, 198, 1)">({</span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)"> providers: [<span style="color: rgba(96, 137, 180, 1)">LoginGuard<span style="color: rgba(197, 200, 198, 1)">, <span style="color: rgba(96, 137, 180, 1)">UnsaveGuard<span style="color: rgba(197, 200, 198, 1)">],</span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)"> bootstrap: [<span style="color: rgba(96, 137, 180, 1)">AppComponent<span style="color: rgba(197, 200, 198, 1)">]</span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">})</span></div>
</div>
<p><span style="font-family: 黑体; font-size: 16px"><strong> 3. 在 app-routing.module.ts 中配置</strong></span></p>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, "Courier New", monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(197, 200, 198, 1)"> { path: <span style="color: rgba(154, 168, 58, 1)">'usersetting'<span style="color: rgba(197, 200, 198, 1)">, component: <span style="color: rgba(96, 137, 180, 1)">UserSettingsComponent<span style="color: rgba(197, 200, 198, 1)">, canActivate: [<span style="color: rgba(96, 137, 180, 1)">LoginGuard<span style="color: rgba(197, 200, 198, 1)">], canDeactivate: [<span style="color: rgba(96, 137, 180, 1)">UnsaveGuard<span style="color: rgba(197, 200, 198, 1)">]},</span></span></span></span></span></span></span></span></span></div>
</div>
<p> </p>
<p> 4. 使用,在设置页面保存按钮,点击后直接离开,不点击离开时提示用户</p>
<p> </p>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, "Courier New", monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(152, 114, 162, 1)">import<span style="color: rgba(197, 200, 198, 1)"> { <span style="color: rgba(96, 137, 180, 1)">UnsaveGuard<span style="color: rgba(197, 200, 198, 1)"> } <span style="color: rgba(152, 114, 162, 1)">from<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(154, 168, 58, 1)">'../loginGuard/unsave.guard'</span></span></span></span></span></span></span></div>
<div> </div>
<div><span style="color: rgba(152, 114, 162, 1)">export<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(152, 114, 162, 1)">class<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(155, 0, 0, 1)">UserSettingsComponent<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(152, 114, 162, 1)">implements<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(199, 68, 74, 1)">OnInit<span style="color: rgba(197, 200, 198, 1)"> {</span></span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)"> saveState<span style="color: rgba(103, 104, 103, 1)">:<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(199, 68, 74, 1)">boolean<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(103, 104, 103, 1)">=<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(64, 128, 128, 1)">false</span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(152, 114, 162, 1)">constructor<span style="color: rgba(197, 200, 198, 1)">(<span style="color: rgba(152, 114, 162, 1)">private<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(96, 137, 180, 1)">unsaveGuard<span style="color: rgba(103, 104, 103, 1)">:<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(155, 0, 0, 1)">UnsaveGuard<span style="color: rgba(197, 200, 198, 1)">) { }</span></span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(206, 103, 0, 1)">save<span style="color: rgba(197, 200, 198, 1)">() {</span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(199, 68, 74, 1)">this<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(152, 114, 162, 1)">saveState<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(103, 104, 103, 1)">=<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(64, 128, 128, 1)">true</span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(199, 68, 74, 1)">this<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(96, 137, 180, 1)">unsaveGuard<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(206, 103, 0, 1)">changeSaveState<span style="color: rgba(197, 200, 198, 1)">()</span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)"> }</span></div>
<div><span style="color: rgba(197, 200, 198, 1)">}</span></div>
</div>
<p> </p>
<p>三 . resolve -- 守卫</p>
<p>(未完待续)</p>
<p> </p>
<p>代码路径访问: https://github.com/kefaze/ang</p>
<p> </p><br><br>
来源:https://www.cnblogs.com/monkey-K/p/11496235.html
頁:
[1]