私のサーバのヘッダにヘッダを使用し、コンポーネントの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];
});
}
}
ここでConfigを指定しますか?エラーメッセージが表示されますか? Observableは、文字列ではなく、購読することができます。 https://angular.io/docs/ts/latest/cookbook/component-communication.html –
ohkを参照してください。文字列を購読することはできません。 – klp
ここでは、すべてのコンポーネントでそのヘッダーを使用したいと思います。任意の解決策 – klp