Angular 2-重定向登录页面

Angular 2-重定向登录页面,angular,typescript,angular2-routing,Angular,Typescript,Angular2 Routing,首先调用模板login(login.component)。登录加载app.component后 我的问题是,这可能吗?我该怎么办 佩尔冈塔·伊迪塔达: 我已经用过了,可以激活了。对不起,我还在学英语。我想要以下的 引导程序首先调用app.componet @组件({ 选择器:“我的应用程序”, 模板:` 佩内尔 仪表板 使用者 `, })使用CanActivate只有当页面在路由中被激活或重定向到登录时,您才能允许用户访问该页面 import { ModuleWithProvide

首先调用模板login(
login.component
)。登录加载
app.component

我的问题是,这可能吗?我该怎么办

佩尔冈塔·伊迪塔达:

我已经用过了,可以激活了。对不起,我还在学英语。我想要以下的

引导程序首先调用app.componet

@组件({
选择器:“我的应用程序”,
模板:`
    佩内尔
    • 仪表板
  • 使用者
`,
})
使用CanActivate只有当页面在路由中被激活或重定向到登录时,您才能允许用户访问该页面

import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { CanActivateAuthGuard } from './auth.service'

import { MyComponent } from './app.component';

const routes: Routes = [
    { path: '/home', component: MyComponent , canActivate: [CanActivateAuthGuard]}]
/============/

对,;这是可能的——文档中对它进行了很好的描述,特别是在本节中

您需要定义一个
CanActivate
guard,然后使用guard保护路线:

auth guard.service.ts

import { Injectable }     from '@angular/core';
import { CanActivate }    from '@angular/router';

@Injectable()
export class AuthGuard implements CanActivate {
  canActivate() {
    console.log('AuthGuard#canActivate called');
    return true;
  }
}
import { AuthGuard }      from '../auth-guard.service';

const adminRoutes: Routes = [
  {
    path: 'admin',
    component: AdminComponent,
    canActivate: [AuthGuard],
    children: [
      {
        path: '',
        children: [
          { path: 'crises', component: ManageCrisesComponent },
          { path: 'heroes', component: ManageHeroesComponent },
          { path: '', component: AdminDashboardComponent }
        ],
      }
    ]
  }
];

export const adminRouting: ModuleWithProviders =
    RouterModule.forChild(adminRoutes);
然后使用防护装置保护站点中需要验证的部分:

管理路由.ts

import { Injectable }     from '@angular/core';
import { CanActivate }    from '@angular/router';

@Injectable()
export class AuthGuard implements CanActivate {
  canActivate() {
    console.log('AuthGuard#canActivate called');
    return true;
  }
}
import { AuthGuard }      from '../auth-guard.service';

const adminRoutes: Routes = [
  {
    path: 'admin',
    component: AdminComponent,
    canActivate: [AuthGuard],
    children: [
      {
        path: '',
        children: [
          { path: 'crises', component: ManageCrisesComponent },
          { path: 'heroes', component: ManageHeroesComponent },
          { path: '', component: AdminDashboardComponent }
        ],
      }
    ]
  }
];

export const adminRouting: ModuleWithProviders =
    RouterModule.forChild(adminRoutes);

不客气,@RenatoSouzadeOliveira--很乐意帮忙。:-)