2017-07-15 88 views
2

我需要這個canActivate在json加載之前不要進一步跳過,爲此我放入一個檢查,如果canActivate不是未定義的,它返回false,但是在控制檯輸出canActivate時傳遞並且與值未定義。我在做什麼是錯的,我會很感激的幫助,謝謝。CanActivate在Angular 2中返回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

+0

檢查我的答案低於 – Aravind

回答

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; 
     }); 
    } 

現在你將得到對象

+0

而地圖()是必要的? – Ivan

+0

是需要的。編輯刪除更新後 – Aravind

+0

這是有幫助或需要更多的幫助? – Aravind