Angular 2 Login指令和location.createComponent

我在我的angular度应用程序上实现一个login系统。 我在这里find了一个很好的资源。 但是在自定义的RouterOutlet指令中有一个错误,如下所示:

import { ElementRef, DynamicComponentLoader, AttributeMetadata, Directive, Attribute } from '@angular/core'; import { Router, RouterOutlet, ComponentInstruction } from '@angular/router-deprecated'; import { UserService } from './user.service'; @Directive({ selector: 'router-outlet' }) export class LoggedInRouterOutlet extends RouterOutlet { publicRoutes: Array; private parentRouter: Router; private userService: UserService; constructor( _elementRef: ElementRef, _loader: DynamicComponentLoader, _parentRouter: Router, @Attribute('name') nameAttr: string, userService: UserService ) { super(_elementRef, _loader, _parentRouter, nameAttr); this.parentRouter = _parentRouter; this.userService = userService; this.publicRoutes = [ '', 'login' ]; } activate(instruction: ComponentInstruction) { if (this._canActivate(instruction.urlPath)) { return super.activate(instruction); } this.parentRouter.navigate(['Login']); } _canActivate(url) { return this.publicRoutes.indexOf(url) !== -1 || this.userService.isLoggedIn() } } 

而错误是: TypeError: location.createComponent is not a function

我读了关于它的事情,问题可能是_elementRef是一个ElementRef对象,超级方法想要一个ViewContainerRef对象。 但是我刚开始学习angular度,我不知道如何解决这个问题。 除此之外,Angular2还在积极开发之中,在教程和我的版本( 2.0.0-rc.1 )之间可能会发生变化,

那么编写我的假设使我find了解决办法:就像导入ViewContainerRef而不是ElementRef一样简单,并在构造函数中获得一个实例:

 constructor( _viewContainerRef: ViewContainerRef, _loader: DynamicComponentLoader, _parentRouter: Router, @Attribute('name') nameAttr: string, userService: UserService ) { super(_viewContainerRef, _loader, _parentRouter, nameAttr); this.parentRouter = _parentRouter; this.userService = userService; this.publicRoutes = [ '', 'login' ]; } 

当然,我不知道自己在做什么,但是如果这样做的话…那些了解他们的东西,并希望增加更多细节的人是受欢迎的。