简体   繁体   English

如何为订阅内的变量赋值?

[英]How to assign a value to variable inside subscription?

I have Ionic 4 angular project, I need to assign value while reading inside subscribe.我有 Ionic 4 angular 项目,我需要在订阅内部阅读时分配值。 However I could not do that.但是我不能那样做。 I know there are several Questions and Answers related to mine.我知道有几个问题和答案与我的有关。 However none of them is working for me.然而,他们都没有为我工作。 Here is the my code and I tried so far.这是我的代码,到目前为止我已经尝试过。 Any comments, any help is appriciated.任何评论,任何帮助都是appriciated。 Thank you very much.`非常感谢。`

Service.ts服务.ts

  public loggedChecker: any;
  private lofff = new Subject<any>();

    postLoginToServer (password, email) {
    var headers = new Headers();
    headers.append("Accept", 'application/json');
    headers.append('Content-Type', 'application/json' );
    let postData =  {
     password: password,
     mail: email
 }
 console.log("8888888888888888888888888888888");

    this.http.post("http://localhost:8080/api/login", postData, { observe: 'response'} )
      .pipe(map(data =>  this.loggedChecker =(data.body)));

      console.log(this.loggedChecker,"0330303333333333"); //undefined

      return this.loggedChecker+"";
}

And here is the another I try这是我尝试的另一个

  postLoginToServer (password, email) {
    var headers = new Headers();
    headers.append("Accept", 'application/json');
    headers.append('Content-Type', 'application/json' );
    let postData =  {
     password: password,
     mail: email
 }
    this.http.post("http://localhost:8080/api/login", postData,{observe: 'response'})
      .subscribe(data => {
         
        this.loggedChecker =(data.body);
        
        this.textToDisplay = (data.body);
        this.lofff.next(data);
       }, error => {
        console.log(error);
      });

    console.log(this.lofff,"logged checker");
    
    return this.loggedChecker+"";
    }

Here is the page.ts这是 page.ts

Page.ts页面.ts

 logForm(){    
   
   console.log(this.fineService.postLoginToServer(this.passwordUser, this.email));
}
    

Both of them are not working.他们两个都没有工作。 Thank you.谢谢你。

Since it's a callback function while calling the post request and since JS is non blocking.因为它是调用 post 请求时的回调函数,并且因为 JS 是非阻塞的。 below lines will be excuted without waiting for the subscribe function call.下面几行将在不等待 subscribe 函数调用的情况下执行。 hence you are getting undefined.因此你变得不确定。

try console logging inside subscribe function or make the service function as async for your flow to wait until request is processed.尝试在 subscribe 函数内进行控制台日志记录,或者使服务函数作为异步流等待请求被处理。

From what I understand you are trying to assign a class property named loggedChecker .据我了解,您正在尝试分配一个名为loggedChecker的类属性。 This is what you should do:这是你应该做的:

  postLoginToServer (password, email) {
    var headers = new Headers();
    headers.append("Accept", 'application/json');
    headers.append('Content-Type', 'application/json' );
    let postData =  {
     password: password,
     mail: email
    }
    this.http.post("http://localhost:8080/api/login", postData,{observe: 'response'})
      .pipe(tap(data => {
        this.loggedChecker =(data.body);
        this.textToDisplay = (data.body);
        this.lofff.next(data);
       }, error => {
        console.log(error);
      }));
    }

tap(import from rxjs) operator will not change the data but can be used to do things like what you're trying to do. tap(import from rxjs) 运算符不会更改数据,但可用于执行您想要执行的操作。

UPDATE: You should change your logForm function.更新:您应该更改 logForm 函数。

 logForm(){    
   console.log(this.fineService.postLoginToServer(this.passwordUser, this.email));
}

should be应该

     logForm(){    
       this.fineService.postLoginToServer(this.passwordUser, this.email).subscribe(data => {
        console.log(data);
       })
    }

because the HTTP request will return an observable and you will get its value when you subscribe to it.因为 HTTP 请求将返回一个 observable,当你订阅它时你会得到它的值。

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

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