0
私はangular2 rc6を使用していますが、最新の発表リリースを使用しても構いません。 認証されたユーザだけにアクセスできるようにルータのアウトレットを実装しようとしています。Angular2:新しいリリースrc6でActivateメソッドを拡張します
私は以前のリリースで動作するこのコードブロックを持っています。
旧コード:私はRC 6で動作しますが、同じ動作を提供するために、「活性化」の方法を実装するためにいくつかの助けを必要とするためにコードを変更しようとしています
import {Directive, Attribute, ElementRef, DynamicComponentLoader} from 'angular2/core';
import {Router, RouterOutlet, ComponentInstruction} from 'angular2/router';
@Directive({
selector: 'router-outlet'
})
export class AuthCheck extends RouterOutlet {
publicRoutes: any;
private parentRouter: Router;
constructor(_elementRef: ElementRef, _loader: DynamicComponentLoader, _parentRouter: Router,
@Attribute('name')nameAttr:string) {
super(_elementRef, _loader, _parentRouter, nameAttr);
this.parentRouter = _parentRouter;
this.publicRoutes = {
'login': true
};
}
activate(instruction: ComponentInstruction) {
let url = instruction.urlPath;
if(!this.publicRoutes[url] && !localStorage.getItem('auth_key')){
this.parentRouter.navigateByUrl('/login');
}
return super.activate(instruction);
}
}
:
変更されたコード:
//import {Directive, Attribute, ElementRef, DynamicComponentLoader} from '@angular/core';
import {Router, RouterOutlet, RouterOutletMap, ActivatedRoute, } from '@angular/router';
import {
Component, Directive, ComponentMetadata, Input, ReflectiveInjector,
ViewContainerRef, Compiler, NgModule, ComponentRef, ComponentFactoryResolver, Injector, ResolvedReflectiveProvider
} from '@angular/core';
@Directive({
selector: 'router-outlet'
})
export class AuthCheck extends RouterOutlet {
publicRoutes: any;
private parentRouter: RouterOutletMap;
constructor(_parentOutletMap: RouterOutletMap, _location: ViewContainerRef, _resolver: ComponentFactoryResolver,
name: string)
//constructor(_elementRef: ViewContainerRef, _loader: any, _parentRouter: Router,
// @Attribute('name') nameAttr: string)
{
super(_parentOutletMap, _location, _resolver, name);
this.parentRouter = _parentOutletMap;
this.publicRoutes = {
'login': true
};
}
activate(activatedRoute: ActivatedRoute, loadedResolver: ComponentFactoryResolver, loadedInjector: Injector, providers: ResolvedReflectiveProvider[], outletMap: RouterOutletMap) {
//implementation
}
//activate(instruction: any) {
// let url = instruction.urlPath;
// if (!this.publicRoutes[url] && !localStorage.getItem('auth_key')) {
// this.parentRouter.registerOutlet.('/login');
// }
// return super.activate(instruction);
//}
}
あなたが働いていたコードを投稿することができますか?その答えはあなたのコードではなく、何かのためのコードであるようです。 –
受け入れられる答えは、ルートの "canActivate"プロパティを使用します。これは、問い合わせ者が解決したい仕事です。しかし、他の理由で "活性化"メソッドを使用する解決策がある場合、私は興味があります。 –