简体   繁体   English

将第一个方法的返回值传递给Typescript for Angular2中的另一个方法

[英]Pass first method return value to another method in Typescript for Angular2

I am new to typescript and would like to pass one method return value to another method. 我是打字稿新手,想将一个方法的返回值传递给另一个方法。 Because second method is dependent on first method return value. 因为第二种方法依赖于第一种方法的返回值。 Below is my code: 下面是我的代码:

// First method call
this.someService.getLoggedinUser()
    .subscribe(uid => {
        console.log(uid);
        this.uid = uid;
    });

//Second method call
this.someService.getUser(this.uid.toUpperCase())
    .subscribe(user => {
        this.user = user;
    });

I am trying to pass mehtod 1 return value to method 2 something like: 我正在尝试将方法1的返回值传递给方法2,例如:

constructor(private someService:SomeService){
this.someService.getUser(uid: string  => {
   this.someService.getLoggedinUser()
      .map(uid => {
          return uid.toUpperCase();
      });
  })
  .subscribe(user => {
        this.user = user;
    }); }

Please help me to achieve this. 请帮助我实现这一目标。

The best method in angular2 You can do this using flatMap angular2中的最佳方法您可以使用flatMap做到这一点

Then here is how you chain two calls: 然后,您将如何链接两个呼叫:

private someMethod{
    this.someService.getLoggedinUser()
       .subscribe(uid => {
               console.log(uid);
               this.uid = uid;
            })
            .flatMap((uid) => 
                this.someService.getUser(this.uid.toUpperCase())
                   .subscribe(user => {
                        this.user = user;
            });
     }

you can assign the value returned by the first method call to a local var and then use it in the next method i guess or you can do the same in the first observable method like this. 您可以将第一个方法调用返回的值分配给本地var,然后在我猜的下一个方法中使用它,或者您可以在第一个可观察的方法中执行相同的操作,如下所示。

i used this in my repo https://github.com/rahulrsingh09/AngularConcepts 我在我的仓库https://github.com/rahulrsingh09/AngularConcepts中使用了这个

getLukeSkywalkerObservable(){
    return this.http.get('http://swapi.co/api/people/1/')
      .map(res => {
        return  res.json(); // using maps to filter data returned form the http call
      }).map(data => {
        return data; // using maps of maps to filter data returned form the map
      }).flatMap((jedi) => this.http.get(jedi.homeworld))
      .map(res => {
        return res.json().name; // using flat maps to combine data returned from two observables into one
      }).catch((error:any) => Observable.throw(error.json().error || 'Server error'));
  }

Here is a simple form: 这是一个简单的形式:

var user;

this.someService.getLoggedinUser()
    .subscribe(uid => this.someService.getUser(uid.toUpperCase())
                      .subscribe(user => this.user = user));

You could just call the second method in the Observable of your element request. 您可以只在元素请求的Observable中调用第二个方法。

public test() {
    this.someService
        .getLoggedinUser()
        .subscribe(uid => {
            const uppercaseUid = uid.toUpperCase();
            this.someService
                .getUser(uppercaseUid)
                .subscribe(user => this.user = user);
        });
}

You can use nested subscribe calls to achieve this. 您可以使用嵌套的订阅调用来实现此目的。

// assigning self = this for debugging purpose when typing variable in console
// example typing self.uid gives value inside subscribe but this.uid is 
//undefined.

let self = this; 
self.someService.getLoggedinUser()
.subscribe(uid => {
    console.log(uid);
    self.uid = uid;
    //Second method call
    self.someService.getUser(uid.toUpperCase())
     .subscribe(user => {
         self.user = user;
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM