2016-08-30 8 views
0

私のサーバのヘッダにヘッダを使用し、コンポーネントの1つに宣言しました。サーバコールはあったが失敗したすべてのコンポーネントでヘッダを使用したいそのヘッダに加入し、誰もがangular2のコンポーネント変数を購読するには

私のインタフェースコンポーネント、

import { Injectable } from '@angular/core'; 
@Injectable() 
export class Config { 

header = 'http://localhost/a2server/index.php/'; 

} 

私main.ts(ここで私は、ヘッダーを使用したい)

var data :any; 
    constructor(public location: Location,public config:Config,public router: Router, public http: Http, fbld: FormBuilder,public toastr: ToastsManager) { 

    this.config 
     .subscribe(header => this.data = data); 
     console.log(this.data) 

私は私を知っている私を助けてくださいすることができます間違っている、いくつかすることができる一つは

の注射、あなたが観測に加入

import { Injectable } from '@angular/core'; 
    import { Http, Response } from '@angular/http'; 
    import { Observable } from 'rxjs/Observable'; 
    import {Component} from '@angular/core'; 
    import { IDetails } from './details'; 
import { Config } from '../headers'; 
@Component({ 
    providers: [Config] 
}) 
@Injectable() 
export class GetCustInfo { 
    header:any; 
    str = localStorage.getItem('social'); 
    loc = JSON.parse(this.str); 
    id = this.loc.profile_id; 
    constructor(private _http: Http,public config:Config) { this.header = this.config.header;} 
private _productUrl = this.header+'profile/editcustominfo/'+this.id; 
getCustList(): Observable<IDetails[]> { 
    return this._http.get(this._productUrl) 

.map((response: Response) => { 

    return <IDetails[]> response.json().data[0]; 

    }); 

} 

}

+0

ここでConfigを指定しますか?エラーメッセージが表示されますか? Observableは、文字列ではなく、購読することができます。 https://angular.io/docs/ts/latest/cookbook/component-communication.html –

+0

ohkを参照してください。文字列を購読することはできません。 – klp

+0

ここでは、すべてのコンポーネントでそのヘッダーを使用したいと思います。任意の解決策 – klp

答えて

0

私を助けてください。単純な文字列の場合は、プロパティにアクセスするだけです。

var data :any; 
    constructor(public location: Location,public config:Config,public router: Router, public http: Http, fbld: FormBuilder,public toastr: ToastsManager) { 

    console.log(this.config.header); 
+0

ありがとうございます....... – klp

+0

私は注射可能なサービスのプロバイダを維持することができます... – klp

+0

申し訳ありませんが、私は質問を理解していません。 h?あなたはどんな問題に遭遇しますか? 「キープ」とはどういう意味ですか? –

関連する問題