简体   繁体   English

属性 pipe 在类型 void Angular 上不存在

[英]Property pipe does not exist on type void Angular

This is my code it gives me error here when i am using first with pipe in angular 10这是我的代码,当我在 angular 10 中首先使用 pipe 时,它在这里给我错误


import { first } from 'rxjs/operators';
import { AuthenticationService } from '../services/authentication.service';

submitForm(){ 
      this.submitted = true;
      if (this.loginForm.invalid) {
        return;
     }

      this.loading = true;
      console.log(this.f.username.value)
      this.executeImportantAction();
      this.authenticationService.login(this.f.username.value, this.f.password.value)
            .pipe(first())                                                           >>> Error here
            .subscribe(
                data => {
                    this.router.navigate([this.returnUrl]);
                },
                error => {
                    this.error = error;
                    this.loading = false;
                });
      }

This is my Login function in authenticateService which is down bellow这是我在下面的 authenticateService 中的登录 function


  login(userId: string, userPassword: string) {
        this.http.post<any>(this.Url,'/' + {userId} + '/' + {userPassword})
            .pipe(map(user => {
                // store user details and jwt token in local storage to keep user logged in between page refreshes
                localStorage.setItem('currentUser', JSON.stringify(user));
                this.currentUserSubject.next(user);
                return user;
            }));
    }

That Error was gone but now i am getting the new error那个错误消失了,但现在我得到了新的错误

 login(userId: string, userPassword: string): Observable<User> {
        this.http.post(this.Url,'/' + {userId} + '/' + {userPassword})
            .pipe(map(user => {
                // store user details and jwt token in local storage to keep user logged in between page refreshes
                localStorage.setItem('currentUser', JSON.stringify(user));
                this.currentUserSubject.next(user);
                return user;
            }));
    }
    

You have to return this.http, right now login is not returning anything (the return inside the map of the http pipe is a return for the pipe, login still returns void)你必须返回这个.http,现在登录没有返回任何东西(http pipe 的map 里面的返回是pipe 的返回,登录仍然返回void)

you could have noticed this if you used TS to its full extent.如果您充分使用 TS,您可能会注意到这一点。 Change your function types to:将您的 function 类型更改为:

login(userId: string, userPassword: string): Observable<User> (you should create an interface for the user, instead of using any) login(userId: string, userPassword: string): Observable<User> (你应该为用户创建一个接口,而不是使用任何接口)

this.http.post<dont-use-any-here>

With types, TS would have warned you with this functions expects to return an observable, but you are returning void对于类型,TS 会警告您this functions expects to return an observable, but you are returning void

login(userId: string, userPassword: string): Observable<User> {
        return this.http.post(this.Url,'/' + {userId} + '/' + {userPassword})
            .pipe(map(user => {
                // store user details and jwt token in local storage to keep user logged in between page refreshes
                localStorage.setItem('currentUser', JSON.stringify(user));
                this.currentUserSubject.next(user);
                return user;
            }));
    }

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

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