简体   繁体   English

Angular 5应用程序在Angular 6上无法正常工作

[英]Angular 5 application is not working properly on Angular 6

I have this method which is working fine in my Angular 5 application: 我有这种方法在我的Angular 5应用程序中正常工作:

registerUser({ value, valid }: { value: UserRegistration, valid: boolean }) {
    this.submitted = true;
    this.isRequesting = true;
    this.errors='';
    if(valid)
    {
        this.userService.register(value.email,value.password,value.firstName,value.lastName,value.location)
                  .finally(() => this.isRequesting = false)
                  .subscribe(
                    result  => {if(result){
                        this.router.navigate(['/login'],{queryParams: {brandNew: true,email:value.email}});                         
                    }},
                    errors =>  this.errors = errors);
    }      
} 

As you can see this will call a method on userService class called register that has defined like this: 如您所见,这将在userService类上调用名为register的方法,该方法已定义如下:

register(email: string, password: string, firstName: string, lastName: string,location: string): Observable<UserRegistration> {
    let body = JSON.stringify({ email, password, firstName, lastName,location });
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    return this.http.post(this.baseUrl + "/account", body, options)
      .map(res => true)
      .catch(this.handleError);
} 

Well, now I want to run the same code on my new Angular 6 application. 好吧,现在我想在新的Angular 6应用程序上运行相同的代码。 The problem is the above code is not running there and there is some problems with map and catch method. 问题是上面的代码未在其中运行,并且map和catch方法存在一些问题。 I have read some similar answers and they suggested to use pipe method. 我读过一些类似的答案,他们建议使用pipe方法。 I have tried the following, but it doesn't work as per my expectation. 我已经尝试了以下方法,但是按我的期望它不起作用。 It successfully calls the post API on the server but it seems the this.router.navigate(['/login'] will never works. Would you please help me how can I use Angular 6 equivalent of the map method here properly? 它成功地在服务器上调用了post API,但似乎this.router.navigate(['/login']将永远无法工作。请您帮我如何正确使用Angular 6等价的map方法?

The Angular 6 version: Angular 6版本:

registerUser({ value, valid }: { value: UserRegistration, valid: boolean }) 
{
    this.submitted = true;
    this.isRequesting = true;
    this.errors = '';
    if (valid) {
      this.userService.register(value.email, value.password, value.firstName, value.lastName, value.location, value.username)
        .pipe(
        finalize(() => this.isRequesting = false))
        .subscribe(
          result => {
            if (result) {
              this.router.navigate(['/login'], { queryParams: { brandNew: true, email: value.email } });
            }
          },
          errors => this.errors = errors);
    }

And: 和:

 register(email: string, password: string, firstName: string, lastName: string, location: string, username: string): Observable<UserRegistration> {

    let body = JSON.stringify({ email, password, firstName, lastName, location, username });
    let headers = new HttpHeaders({ 'Content-Type': 'application/json' });

    return this.http.post<UserRegistration>(this.baseUrl + "/account", body, { headers: headers }).pipe(
      map(res =>  res ));
  }

Just like that? 就这样吗

return this.http.post<UserRegistration>(this.baseUrl + "/account", body, { headers: headers }).pipe(
      map(res => true));
  }

And the method's signature should be Observable<boolean> or Observable<true> . 并且方法的签名应为Observable<boolean>Observable<true> However, I don't understand why you need to return a boolean here. 但是,我不明白为什么您需要在此处返回boolean

Update: I would write the method 更新:我会写方法

register(userRegistration: UserRegistration): Observable<{}> {
    let headers = new HttpHeaders({ 'Content-Type': 'application/json' });

    return this.http.post(this.baseUrl + "/account", userRegistration, { headers });
}

And for the caller: 对于呼叫者:

      this.userService.register(value).pipe(
        finalize(() => this.isRequesting = false)
      )
      .subscribe(
         () => this.router.navigate(['/login'], { queryParams: { brandNew: true, email: value.email } }),
         (error: HttpErrorResponse) => this.errors = xxx(error) // your error handler
      );

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

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