Javascript 防止在angular 4中的浏览器中重定向写入url
我无法将用户踢出到根url 以下代码不起作用 路由器Javascript 防止在angular 4中的浏览器中重定向写入url,javascript,node.js,angular,typescript,routes,Javascript,Node.js,Angular,Typescript,Routes,我无法将用户踢出到根url 以下代码不起作用 路由器 导出常量路由定义:路由=[ { 路径:“家”, 组件:LandingPageComponent, canActivate:[AuthGuard] }, { 路径:“福尔摩斯”, 组件:Holmesmaster组件, canActivate:[AuthGuard], 儿童:[{ 路径:“未授权”, 组件:未经授权的组件 }] }, { 路径:'他的', 组件:主组件, canActivate:[AuthGuard], 儿童:[{ 路径:“”,
导出常量路由定义:路由=[
{
路径:“家”,
组件:LandingPageComponent,
canActivate:[AuthGuard]
},
{
路径:“福尔摩斯”,
组件:Holmesmaster组件,
canActivate:[AuthGuard],
儿童:[{
路径:“未授权”,
组件:未经授权的组件
}]
},
{
路径:'他的',
组件:主组件,
canActivate:[AuthGuard],
儿童:[{
路径:“”,
组件:HisParentComponent
},
{
路径:“组”,
组件:GroupListComponent
},
{
路径:“测试”,
组件:TestComponent
},
]
},
{
路径:'**',
组件: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;
}
}
从'@angular/core'导入{Injectable};
从'@angular/Router'导入{Router,CanActivate,ActivatedRouteSnapshot,RouterStateSnashot};
从“../Services/auth.service”导入{AuthService}
从“../Content/Config/Appconfig”导入{Appconfig};
从“rxjs/Observable”导入{Observable};
@可注射()
导出类AuthGuard实现了CanActivate{
未授权代码:编号=401;
构造函数(专用路由器:路由器,专用authService:authService){}
canActivate(路由:ActivatedRouteSnapshot,状态:RouterStateSnashot):可观察的|承诺|布尔{
this.someservice.authenticateUserLocalDB();
返回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>
-
-
{{child2.MenuName}
可能尝试使用重定向到:
{
path: 'home',
component: LandingPageComponent,
canActivate: [AuthGuard]
},
{
path: '**',
redirectTo: 'home'
}
编辑您可能还更喜欢使用空路径重定向:我这样做/{path:'',组件:LandingPageComponent,canActivate:[AuthGuard]}{path:'',重定向到:'home',pathMatch:'full'},{path:'',重定向到:'}但如果我输入了无效的url,它将变为空白页即使它没有重定向,如果我输入了正确的url,我想阻止用户在浏览器中输入url和重定向我认为canactivate应该有一些更精确的逻辑编辑答案