Angular 2 return 中的 CanActivate 未定义

CanActivate in Angular 2 return undefined

我需要 canActivate 在 json 加载之前不会进一步跳过,为此我检查了 canActivate 是否未定义,它返回 false,但在控制台中它输出 canActivate 已通过并且值未定义。我做的不对,不胜感激,谢谢。

我的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;
    }
}

]2

在您订阅回复之前,您的数据将为 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; 
        });
    }

现在你将得到对象