angular2 应用程序中所有路由调用的默认方法是什么?

What is the default method that would be called for all routes in angular2 app?

我在app.module.ts、

中有以下路由配置
{
    path: 'login',
    component: loginComponent,
    canActivate: [loggerService]
}, {
    path: '',
    component: AppComponent,
    canActivate: [loggerService]
}, {
    path: '/home',
    redirectTo: ''
}, {
    path: '/blog',
    component: BlogComponent,
    canActivate: [loggerService]
}

当任何路由被调用时,我想要一个方法运行。例如,记录访问或预先请求一些数据。是否有任何默认的用户定义或内置方法可用于实现此目的?

Yes.There 是一种让 Auth guard implementation.Below 是例如:

    import { Injectable } from '@angular/core';
    import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild, Router } from '@angular/router';        

    @Injectable()
    export class AuthGuard implements CanActivate, CanActivateChild {

      constructor(private router: Router) {
      }

      canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
        if (condition)) {
          return true;
        }
        this.router.navigate(['/unauthorized']);
        return false;
      }

      canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
        return this.canActivate(route, state);
      }
    }

在你的路线中:

import { Routes, RouterModule } from '@angular/router';
import { AuthGuard } from 'Your Location/auth-guard.service';

const ROUTES: Routes = [
  {
    path: '',
    canActivateChild: [AuthGuard],
    children: [
      {path: '', component: CardbrandComponent}     
      ]

  }
];