[英]Create a login page avoiding the other pages
I'm quite new on angular and I was creating a login page, my problem is I'd like to create a login page without show any other components besind and once I got the response 200 redirect to the other component:我对角度很陌生,我正在创建一个登录页面,我的问题是我想创建一个登录页面而不显示任何其他组件,一旦我得到响应 200 重定向到另一个组件:
my app.component.html is:我的 app.component.html 是:
<nav>
<p-tabView (onChange)="onClick($event)">
<p-tabPanel header="Random " routerLink="/random" >
</p-tabPanel>
<p-tabPanel header="Manifacture" routerLink="/Manifacture">
</p-tabPanel>
<p-tabPanel header="Add" routerLink="/add">
</p-tabPanel>
<router-outlet></router-outlet>
</p-tabView>
</nav>
my app.router:我的 app.router:
{ path: '', redirectTo: '/login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'random', component: RandomPComponent },
{ path: 'Manufacuter', component: ManufactureComponent }
....
What can I see on my angular is:我可以在我的角度上看到的是:
I know the problem is my main page with the tab-view componet but I want are invisible until is logged in, and I'd like to know if is correct to add the tab-view in the app.component or shoud I create another component (and an other router) to show as I want我知道问题是我的带有选项卡视图组件的主页,但我希望在登录之前不可见,我想知道在 app.component 中添加选项卡视图是否正确,或者我应该创建另一个组件(和其他路由器)按我的意愿显示
Thanks to all, I really appreciate谢谢大家,我真的很感激
In my Opinion the best way is to use the ngIf directive and display your tabView when you user is log using a boolean value.在我看来,最好的方法是使用ngIf 指令并在用户使用布尔值登录时显示你的 tabView。
Concerning you app-component, i think that is better to have another component like a layout and only let your router outlet on app-comp关于你的应用程序组件,我认为最好有另一个组件,比如布局,只让你的路由器在应用程序组件上出口
I suggest you use route guards to redirect to login page我建议你使用 路由守卫重定向到登录页面
export class IsLoggedGuard implements CanActivate {
constructor(private loginService: LoginService,
private router: Router,
private activatedRouter: ActivatedRoute) { }
canActivate(activatedRouteSnapshot:ActivatedRouteSnapshot,
routerStateSnapshot: RouterStateSnapshot): Observable<boolean> {
return this.loginService.isLogged().pipe(
map((res: any) => {
...
return res.success;
}),
tap(res => {
if (!res)
{
this.router.navigate(['/login'],
{ queryParams: { returnUrl: routerStateSnapshot.url } });
}
}))
}
}
Then you can to have a app.component然后你可以有一个 app.component
<router-outlet></router-outlet>
And a main.component like your "old app.component"还有一个 main.component 像你的“旧 app.component”
<nav>
<p-tabView (onChange)="onClick($event)">
....
<router-outlet></router-outlet>
</p-tabView>
</nav>
So, you can use a router like因此,您可以使用路由器,例如
{ path: 'login', component: LoginComponent },
{ path: '', component:MainComponent, canActivate: [IsLoggedGuard ]
children:[
{ path: 'register', component: RegisterComponent },
{ path: 'random', component: RandomPComponent },
{ path: 'Manufacuter', component: ManufactureComponent }
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.