javascript - 防止重定向在 Angular 4 中的浏览器中写入 url
问题描述
我无法将用户踢出到根 url
以下代码不起作用
路由器
export const RouteDefinitions: Routes = [
{
path: 'home',
component: LandingPageComponent,
canActivate: [AuthGuard]
},
{
path: 'holmes',
component: HolmesMasterComponent,
canActivate: [AuthGuard],
children: [{
path: 'unauthorized',
component: UnauthorizedComponent
}]
},
{
path: 'his',
component: MasterComponent,
canActivate: [AuthGuard],
children: [{
path: '',
component: HisParentComponent
},
{
path: 'group',
component: GroupListComponent
},
{
path: 'test',
component: TestComponent
},
]
},
{
path: '**',
component: LandingPageComponent,
canActivate: [AuthGuard]
}
];
我需要做些什么改变才能让它发挥作用?
auth.guard.ts
import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import{ AuthService} from '../Services/auth.service'
import { Appconfig } from '../Content/Config/AppConfig';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class AuthGuard implements CanActivate {
UnauthorizedCode:number=401;
constructor(private router: Router,private authService:AuthService ) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
this.someservice.authenticateUserLocalDB();
return true;
}
}
请帮我
菜单重定向
<div id="sidebar-wrapper">
<aside id="sidebar" class="main-sidebar">
<section class="sidebar">
<ul class="sidebar-menu" id="sidemenu">
<li class="treeview" *ngFor="let menu of menuData">
<!-- Visible for only Menu with no child items -->
<a routerLink="{{menu.Url}}" routerLinkActive="active" *ngIf="menu.childMenus.length==0">
<i class="{{menu.Img}}"></i>
<span style="color: #cccccc !important;">{{menu.MenuName}}</span>
</a>
<!-- Visible for only Menu has child items -->
<a href="#" *ngIf="menu.childMenus&& menu.childMenus.length>0">
<i class="{{menu.Img}} "></i> <span style="color: #cccccc !important">{{menu.MenuName}}</span>
<!--Siddappa Testing-->
<span class="pull-right-container">
<!--<i class="fa fa-angle-right pull-left"></i>-->
<i class="fa fa-angle-right" onClick="($(this)[0].className == 'fa fa-angle-right')?$(this)[0].className='fa fa-angle-down':$(this)[0].className='fa fa-angle-right'" style="float: right !important;"></i>
</span>
</a>
<ul class="treeview-menu" style="display: none;" *ngIf="menu.childMenus.length>0">
<li *ngFor="let child1 of menu.childMenus">
<a routerLink="{{child1.Url}}" *ngIf="child1.childMenus.length==0">
<i class="{{child1.Img}}"></i>
{{child1.MenuName}}
</a>
<a href="#" *ngIf="child1.childMenus.length > 0"><i class="{{child1.Img}}"></i> {{child1.MenuName}}
<span class="pull-right-container">
<i class="fa fa-angle-right pull-left"></i>
</span>
</a>
<ul class="treeview-menu" style="display: none;" *ngIf="child1.childMenus.length>0">
<li class="treeview" *ngFor="let child2 of child1.childMenus">
<a routerLink="{{child2.Url}}"><i class="fa fa-circle-o"></i> {{child2.MenuName}}
</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</section>
</aside>
</div>
解决方案
也许尝试使用redirectTo
:
{
path: 'home',
component: LandingPageComponent,
canActivate: [AuthGuard]
},
{
path: '**',
redirectTo: 'home'
}
编辑您也可能更喜欢使用空路径重定向:
推荐阅读
- visual-studio-code - 如何在 VSCode 上的虚拟 venv 中获得安装在 PyGObject 库中的智能感知、自动完成功能?
- qt - QML TextField 不会根据 cursorPosition 定位文本
- python - 如何在 xml 内容(原子提要)中获取 href 属性值?
- flutter - 在 Firebase 中创建用户集合文档
- json - 从 JSON 中查找 MongoDB 中的平均值
- flutter - 颤振如何在另一个小部件中编辑PageView中的索引并且主索引自动更改?
- javascript - 如何在 2sxc 模块中保存/加载自定义输入的值?
- flutter - Flutter dart 类最终元素和用于更新或普通元素的新元素
- javascript - 如何在 javascript 中读取函数表达式语法?
- python - 在子窗口 tkinter 中打开文件对话框时,根窗口跳转到前面