authentication - LoggedInOutlet angular2 身份验证 - Router v3.0.0-alpha8 - ComponentInstruction 在哪里?

标签 authentication angular angular2-routing angular2-router angular2-router3

我正在使用这样的代码来扩展 RouterOutlet 并创建应用程序范围的身份验证和路由保护

import {Directive, Attribute, ViewContainerRef, DynamicComponentLoader} from '@angular/core';
import {Router, ComponentInstruction} from '@angular/router';
import {Router} from '@angular/router';
import {RouterOutletMap} from '@angular/router/src/router_outlet_map';
import {RouterOutlet} from '@angular/router/src/directives/router_outlet';

import {Authentication} from '../common/authentication.service';

@Directive({
  selector: 'router-outlet'
})
export class LoggedInRouterOutlet extends RouterOutlet {
  publicRoutes:any;
  isAuthenticated:boolean;
  //private router: any;

  constructor(public _elementRef: ElementRef, public _loader: DynamicComponentLoader,
              public _parentRouter: Router, @Attribute('name') nameAttr: string, public authService:Authentication) {
    super(_elementRef, _loader, _parentRouter, nameAttr);
    this.isAuthenticated = authService.isLoggedIn();


    //this.router = _parentRouter;
    /**
     * DEFINE PUBLIC ROUTES
     *
     * The Boolean following each route below denotes whether the route requires authentication to view.
     *
     * Format: key/value pair
     * - key is the /route url "/login", "/signup", etc
     * - value is a boolean true/false
     *    `true` means it's a publicly available route. No authentication required
     *    `false` means it's a protected route which is hidden until user is authenticated
     *
     */
    this.publicRoutes = {
      'login': true,
      'signup': true,
      '404': true
    };
  } // end constructor

  routeIsActive(routePath:string) {
    return this.router.url == routePath;
  }

  activate(instruction: ComponentInstruction) {
    // let url = instruction.urlPath;
    let url = this.router.url;
    // If the url doesn't match publicRoutes and they are not authenticated...
    if (!this.publicRoutes[url] && !this.isAuthenticated) {
      // todo: redirect to Login, may be there a better way?
      this.router.navigateByUrl('/login');
    }
    return super.activate(instruction);
  }
}

问题是新的 v3.0.0-alpha8 路由器中不存在 ComponentInstruction,并且 super 方法签名已更改。如何更新它以在新路由器中工作?我找不到任何解释这些更改的文档。

最佳答案

ComponentInstruction已被弃用。在 Angular2 当前的 RC4 版本中,此类已被列为 reouter-deprecated。随着 RC5 的到来,这个包将被删除。

随着时间的推移,RouterOutlet 发生了很大变化,为了使您的 LoggedInRouterOultet 类正常工作,您必须使用 CanActivate界面。

你可以这样做:

拥有像此处所示的 LoggedInActivator 这样的可注入(inject)服务:

import { Injectable } from '@angular/core';
import { Router, CanActivate } from '@angular/router';
import { LogInService } from './login.service';

@Injectable()
export class LoggedInActivator implements CanActivate {
  constructor(private loginService: LogInService) {}

  canActivate() {
    return this.loginService.isLoggedIn();
  }
}

定义路由时添加 canActivate 并将其映射到组件上的 LoggedInActivator:

{ path: 'home', component: HomeComponent, canActivate: [LoggedInActivator] }

希望这会有所帮助!

关于authentication - LoggedInOutlet angular2 身份验证 - Router v3.0.0-alpha8 - ComponentInstruction 在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38127746/

相关文章:

node.js - 无法从 mongojs 连接,但命令行正常

java - 我如何使用 java 在不进行身份验证的情况下发送电子邮件

angular - 如何在 Angular Material 的多选下拉菜单中实现搜索功能

javascript - Angular 2 最终发布路由器单元测试

javascript - 快照参数/服务在 Angular 2 中返回 NaN

html - 单个页面上的不同导航栏 angular2

node.js - 如何使用 htaccess 的替代方法对 nodejs 应用程序进行密码保护

如果用户已登录,则 Angular 6 更改组件

javascript - 在我的应用程序 angular2 aspnet core 中找不到错误 angular2-cookies/core.js

angular - AngularFire2 toPromise() 方法的异步/等待不起作用?