CanActivate在Angular 2中返回undefined

时间:2017-07-15 17:38:31

标签: javascript angular typescript frameworks canactivate

我需要canActivate在json加载之前不要进一步跳过,为此我检查了canActivate是否未定义,它返回false,但是在控制台中输出canActivate传递并且值为undefined。我正在做的事情是错的,我会感激你的帮助,谢谢。

我的http-data.service

import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import {Response} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from "@angular/router";

@Injectable()
export class HttpService implements CanActivate{
    constructor(private http: Http) {}

    dataModules = this.getDataModules();
    dataPresets = this.getDataPresets();
    dataModuleItems = this.getDataModuleItems();
    data: any[];

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) : Observable<boolean> | boolean {
            if (this.getDataModules !== undefined) {
                console.log('canActivate');
                console.log(this.getDataModules());
                return true;
            } else return false;
    }

    getDataOrganizations(): Observable<any[]>{
        return this.http.get('http://localhost:3010/data')
            .map((resp:Response)=>{
                let dataOrganizations = resp.json().organization;
                return dataOrganizations;
            });
    }

    loadDataModules(): Observable<any[]> {      
        return this.http.get('http://localhost:3010/data')
            .map((resp: Response)=> {
                let dataModules = resp.json().modules;
                return dataModules;
            });
    }

    loadDataPresets(): Observable<any[]> {      
        return this.http.get('http://localhost:3010/data')
            .map((resp: Response)=> {
                let dataPresets = resp.json().presets;
                return dataPresets;
            });
    }

    loadDataModuleItems(): Observable<any[]> {      
        return this.http.get('http://localhost:3010/data')
            .map((resp: Response)=> {
                let dataModuleItems = resp.json().module_items;
                return dataModuleItems;
            });
    }

    loadData() {
        return this.http.get('http://localhost:3010/data')
            .map((resp: Response)=> {
                let data = resp.json();
                return data;
            });
    }

    getDataModules(): any[] {
        this.loadDataModules().subscribe(((modules)=>{this.dataModules = modules; console.log('в http modules');console.log(this.dataModules);}));
        return this.dataModules;
    }


    getDataPresets(): any[] {
        this.loadDataPresets().subscribe(((presets)=>{this.dataPresets = presets; console.log(this.dataPresets);}));
        return this.dataPresets;
    }


    getDataModuleItems(): any[] {
        this.loadDataModuleItems().subscribe(((moduleItems)=>{this.dataModuleItems = moduleItems; console.log(this.dataModuleItems);}));
        return this.dataModuleItems;
    }
}

canActivate: undefined[![][1]] 2

enter image description here

1 个答案:

答案 0 :(得分:0)

在您订阅回复之前,您的数据将为undefined

 getDataOrganizations(): Observable<any[]>{
    let dataOrganizations :any[]
        return this.http.get('http://localhost:3010/data')
            .map(response => response.json().organization)
            .subscribe(data =>  {       
                dataOrganizations = data;
                return dataOrganizations; 
        });
    }

现在你将获得对象