类型“可观察的未知”是指类型“可观察的未知”。不能分配给类型“Observable<{token:string;” }>”。点击错误

发布于 2025-01-10 03:08:07 字数 1311 浏览 0 评论 0 原文

添加令牌

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
import { User } from './interfaces';

@Injectable()
export class AuthService {

  private token = null

  constructor(private http: HttpClient) {
  }

  login(user: User): Observable<{token: string}> {
    return this.http.post<{token: string}>('/api/auth/login', user)
     .pipe(tap(({token}) => {
        localStorage.setItem('auth-token', token)
        this.setToken(token)
     }))
  }

  setToken(token: string) {
    this.token = token
  }
}

我正在尝试通过 .pipe 方法links.ts

export interface User {
  email: string,
  password: string
}

一切都可以使用此代码

  login(user: User): Observable<{token: string}> {
    return this.http.post<{token: string}>('/api/auth/login', user)
  }

当我尝试通过 .pipe 方法 tap 添加令牌时,出现错误

The type "Observable<unknown>" cannot be assigned to the type "Observable<{token:string; }>".
The "token" property is missing in the "{}" type and is mandatory in the "{token: string; }" type.ts(2322)

.pipe 中的任何添加的方法(tap、map 和其他)一旦我从“rxjs/operators”导入它们,就会出现此错误。

I'm trying to add a token via the .pipe method

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
import { User } from './interfaces';

@Injectable()
export class AuthService {

  private token = null

  constructor(private http: HttpClient) {
  }

  login(user: User): Observable<{token: string}> {
    return this.http.post<{token: string}>('/api/auth/login', user)
     .pipe(tap(({token}) => {
        localStorage.setItem('auth-token', token)
        this.setToken(token)
     }))
  }

  setToken(token: string) {
    this.token = token
  }
}

interfaces.ts

export interface User {
  email: string,
  password: string
}

Everything works with this code

  login(user: User): Observable<{token: string}> {
    return this.http.post<{token: string}>('/api/auth/login', user)
  }

When I try to add a token via the .pipe method tap i have a Error

The type "Observable<unknown>" cannot be assigned to the type "Observable<{token:string; }>".
The "token" property is missing in the "{}" type and is mandatory in the "{token: string; }" type.ts(2322)

Any added method in the .pipe (tap, map and others) as soon as I import them from 'rxjs/operators', this error appears.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如若梦似彩虹 2025-01-17 03:08:07

很奇怪你收到这个错误并且我无法重现它,但这应该可以解决你的问题:

login(user: User): Observable<{token: string}> {
  return this.http.post<{token: string}>('/api/auth/login', user)
   .pipe(tap(({token}) => {
     localStorage.setItem('auth-token', token)
     this.setToken(token)
 })) as Observable<{token: string}>
}

it's very strange that you get this error and i can't reproduce it, but this should fix your problem:

login(user: User): Observable<{token: string}> {
  return this.http.post<{token: string}>('/api/auth/login', user)
   .pipe(tap(({token}) => {
     localStorage.setItem('auth-token', token)
     this.setToken(token)
 })) as Observable<{token: string}>
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文