2016-06-21 14 views
0

私はURLでjsonファイルを持っています。 Alosには、ConfigurationService.tsというメソッドとgetConfiguration(key)というURLを取得するサービスがあります。正しい方法でgetUrlを購読する方法。角2

apiは次のように動作するはずです:gets url、そして、現在のurlでverifyLogin()を実行した後。 しかし、私は購読者に問題があります。私は単純な方法があると思います。ここ

import {Injectable} from '@angular/core'; 
import {Http, Response} from '@angular/http'; 
import {Headers, RequestOptions} from '@angular/http'; 
import {Observable} from 'rxjs/Observable'; 

@Injectable() 
export class ConfigurationService { 
constructor(private http:Http) { 
} 
private result: Object; 
getConfiguration(key) { 
    return this.http.get('./app/config/config.json').map((res: Response) => { 
     this.result = res.json(); 
     return this.result[key]; 
    }); 
} 
} 

は、認証サービスである:ここ はconfigurationService.tsです念の

import {Injectable} from '@angular/core'; 
import {Http, Response} from '@angular/http'; 
import {Headers, RequestOptions} from '@angular/http'; 
import {Observable} from 'rxjs/Observable'; 
import {HttpBaseClass} from './http_base_class'; 
import {ConfigurationService} from '../config/configuration_service'; 

@Injectable() 

export class AuthenticationService extends HttpBaseClass { 
result: { 
    ok: boolean; 
}; 



private verifyUrl = ''; 
private logoutUrl = ''; 

constructor (private http: Http, private configurationService: ConfigurationService) { 
    super(http); 
} 


private authRequest (url) { 
    let body = JSON.stringify({}); 
    let headers = new Headers({ 'Content-Type': 'application/json'}); 
    let options = new RequestOptions({ 
     headers: headers 
    }); 
    return this.http.post(url, body, options) 
     .map((res: Response) => res.json()) 
     .map(res => { 
      this.result = res; 
      return this.result.ok; 
     }); 
} 
//test - how to put received url into this.authRequest(this.verifyUrl) ?? 
// private x = this.configurationService.getConfiguration("verifyUrl").subscribe((result) => console.log(result)); 

//verify runs in appComponent oninit. 
verifyLogin() { 
    return this.authRequest(this.verifyUrl); 
} 
logout() { 
    return this.authRequest(this.logoutUrl); 
} 

}

HomeComponent.ts:

ngOnInit() { 
    // check isLogged in 
    this.isLogged(); 
} 
//check if logged in 
isLogged() { 
    this.authenticationService.verifyLogin().subscribe((result) => { 
     if (result) { 
      this.structureRequest.sendRequest().subscribe((result) => this.viewNodes(result)); 
      //makes http request and puts result into contantArray 
     } else if (result === false) { 
      this.router.navigate(['/login']); 
     } 
    }); 
} 

UPDATE:私は「 次の方法でverifyLogin()メソッドを設定しようとしました。しかし、エラーが表示されます:あなたは、動的に設定データをロードするためにキャッシュとコールバックを使用して構成サービスを書き換えることができ

verifyLogin() { 
    return this.configurationService.getConfiguration("verifyUrl") 
     .subscribe((url) => { 
      // this.verifyUrl = url; 
      this.authRequest(url); 
     }); 
} 

答えて

1

:「例外TypeError。this.authenticationService.verifyLogin(...)購読は関数ではありません」。

@Injectable() 
export class ConfigurationService { 
    private _filePath: string = './src/config.json'; 
    private _configCache: any = null; 

    constructor(private _http: Http) { } 

    getConfig(key: string, callback: (value) => void) { 
    if (this._configCache) { 
     return callback(this._configCache[key]); 
    } 

    this._http.get(this._filePath) 
     .map(res => res.json()) 
     .subscribe(
     (data) => { 
      this._configCache = data; 
      callback(this._configCache[key]); 
     }, 
     (error) => { 
      console.log("Couldn't load config."); 
     }, 
     () => { console.log(this._configCache); } 
    ); 
    } 
} 

ので、同じようにそれを使用します。

verifyLogin(callback: (data) => void) { 
    this.configService.getConfig("verifyUrl", (value) => { 
     this.authRequest(value).subscribe((data) => callback(data)); 
    }); 
} 

あなたisLogged方法:

//check if logged in 
isLogged() { 
    this.authenticationService.verifyLogin((result) => { 
     if (result) { 
      this.structureRequest.sendRequest().subscribe((result) => this.viewNodes(result)); 
      //makes http request and puts result into contantArray 
     } else if (result === false) { 
      this.router.navigate(['/login']); 
     } 
    }); 
} 

Plunker for example usage

+0

すぐにお試しください。あなたはthis.verifyUrl = urlがveryfiLogin()より先に実行されると思いますか? – Serhiy

+0

さて、競合状態が発生する可能性があります。私は別の解決策を提案します。 – rinukkusu

+0

が動作していません。メソッドの検証時にURLがまだ空です。 – Serhiy

関連する問題