Angular 2等到变量设置

时间:2022-06-15 12:02:27

I need to get values for my variables via http.get (must be async):

我需要通过http.get获取变量的值(必须是异步):

@Injectable()
export class interaction{
private host: string="";
private port: string = "";
constructor(private http: Http) {

    this.http.get("/interaction.json").subscribe((data: Response) => {
        this.host = data.json().host;
        this.port = data.json().port;

    });  
}

As the data come asyncronously I cannot access them in method below:

由于数据是异步的,我无法通过以下方法访问它们:

interact(data:Request): Promise<Response>{       
    return this.http.post("http://" + this.host + ":" + this.port, data)
        .toPromise()
        .then(data => data.json() as Response)
        .catch(this.handleError);
}

So, the host and port are undefined when i call interact() from another service. Is it possible to await for this variables are set, and then perform post request? I wouldn't like to make get call inside interact(), as there can be many functions using those vars.

因此,当我从另一个服务调用interact()时,主机和端口是未定义的。是否可以等待设置此变量,然后执行post请求?我不想在interact()内部进行调用,因为使用这些变量可以有很多函数。

I'd appreciate any advice.

我很感激任何建议。

1 个解决方案

#1


0  

i think one way would be like this: you can create a subject and use it as a messenger... :

我认为一种方式是这样的:你可以创建一个主题并将其用作信使...:

@Injectable()
export class interaction{
private host: string="";
private port: string = "";
messenger$: Subject<boolean> = new Subject();
constructor(private http: Http) {

    this.http.get("/interaction.json").subscribe((data: Response) => {
        this.host = data.json().host;
        this.port = data.json().port;
        this.messenger$.next(true)
    });  
}

and inside the interact (if it's another class, first inject the interaction service and then) do this:

并在内部交互(如果它是另一个类,首先注入交互服务然后)执行此操作:

interact(data:Request): Observable<any>{ 
    return this.messenger$ //or this.interaction.messenger$
        .switchMap(
            (bool: boolean)=>{
                if(bool){
                    return this.http.post("http://" + this.host + ":" + this.port, data)

                }
                else{
                    // return anything that would help your situation like: Observable.of(null)
                }
            }
        )
}

and later call toPromise or... on the result

然后在结果上调用促销或...

#1


0  

i think one way would be like this: you can create a subject and use it as a messenger... :

我认为一种方式是这样的:你可以创建一个主题并将其用作信使...:

@Injectable()
export class interaction{
private host: string="";
private port: string = "";
messenger$: Subject<boolean> = new Subject();
constructor(private http: Http) {

    this.http.get("/interaction.json").subscribe((data: Response) => {
        this.host = data.json().host;
        this.port = data.json().port;
        this.messenger$.next(true)
    });  
}

and inside the interact (if it's another class, first inject the interaction service and then) do this:

并在内部交互(如果它是另一个类,首先注入交互服务然后)执行此操作:

interact(data:Request): Observable<any>{ 
    return this.messenger$ //or this.interaction.messenger$
        .switchMap(
            (bool: boolean)=>{
                if(bool){
                    return this.http.post("http://" + this.host + ":" + this.port, data)

                }
                else{
                    // return anything that would help your situation like: Observable.of(null)
                }
            }
        )
}

and later call toPromise or... on the result

然后在结果上调用促销或...