简体   繁体   English

创建一个登录页面,避开其他页面

[英]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.

相关问题 Angular 2登录页面,与其他页面的设计不同 - Angular 2 login page that has different design from other pages 错误:在 Angular 2 中找不到要加载的主要出口(登录页面和所有其他页面) - Error: Cannot find primary outlet to load (login page and all other pages) in Angular 2 如何在角度4中创建从应用程序组件页面到其他页面的导航链接? - How to Create Navigation link from app component page to other pages in angular 4? 我是 Angular 的新手。 我必须如何做一个主题的登录页面和具有不同主题的所有其他页面 - I am new to Angular. How do I have to do login page a theme and all other pages with a different theme 如何在 Angular 应用程序中创建两个登录页面 - How to create two login pages in angular application 如何加载创建登录页面 - How to load a create a login page 如何隐藏 Angular 中其他页面的登录和注册链接 - How to hide Login and Register link to other pages in Angular Angular 2中的登录页面(一个用于登录,另一个用于其他布局)的不同布局 - Different layouts for Login Page (one for login and other layout for others) in Angular 2 从登录页面到其他页面的角度路由 - Angular routing from login page onto further pages 像静态主页和其他动态页面一样的wordpress,角度为4 - wordpress like static home page and other dynamic pages in angular 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM