'string' 类型的参数不可分配给类型 '{ email: string; }' NGXS

Argument of type 'string' is not assignable to parameter of type '{ email: string; }' NGXS

我正在尝试使用 ngxs this.store.dispatch(new GetUser(userEmail)) 从 api 获取数据 我尝试通过从 localstorage 存储的用户 ID 作为字符串并将其转换为数字..我得到类似的错误(Argument of type 'string' is not assignable to parameter of type '{ userId: number; }' )...现在我试图通过电子邮件作为字符串获取用户...我收到错误 'string' 类型的参数不可分配给类型为 '{ email: string; 的参数}' NGXS


import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import {  Select, Store } from '@ngxs/store';
import { Observable } from 'rxjs';
import { UserProfile } from 'src/app/core/interfaces/userProfile';
import { GetUser } from './state/profile.action';
import { ProfileState } from './state/profile.state';
import { UserService } from './user.service';

@Component({
  selector: 'app-profile',
  templateUrl: './profile.component.html',
  styleUrls: ['./profile.component.css']
})

export class ProfileComponent implements OnInit {
  @Select(ProfileState.userProfile) userProfile$!: Observable<UserProfile>;

  constructor(private store:Store,private router:Router,private userService:UserService) { }
  ngOnInit() {
    this.getUser();
  }
  getUser() {
     let userEmail = localStorage.getItem('email') || '';
      this.store.dispatch(new GetUser(userEmail)).subscribe((data) => {
        this.router.navigate(['/profile']);
  });
      this.userService.getUser(userEmail).subscribe((response) => (
      console.log(response)
    ));
}
  }

这是state/action代码

  static readonly type = '[Profile] getUser';
  constructor(public payload: { email : string }) {}
}
export class ProfileStateModel {
  userProfile: UserProfile|undefined;
}

@State<ProfileStateModel>({
  name: 'profile',
  defaults: {
    userProfile:undefined,
  }
})
@Injectable()
export class ProfileState {
  profile!: UserProfile;

@Selector()
static userProfile(state: ProfileStateModel) {
  return state.userProfile;
}
constructor(private userService: UserService) {}

@Action(GetUser)
getUser(ctx: StateContext<ProfileStateModel>, action: GetUser ){
  const state = ctx.getState();
  return this.userService.getUser(action.payload.email).pipe(
      tap((profile) => {
        ctx.setState({
          ...state,
          userProfile:profile
        });
        ctx.dispatch(new GetUser(this.profile));
      })
    );
    }}

这是class

export class UserProfile {
  id!: number;
  username!: string ;
  password!: string ;
  email!:string;
  name!: string ;
  roles!: Roles;
  token!: string ;
  cart!:Cart;
}

和服务

@Injectable({
  providedIn: 'root'
})

export class UserService {
  constructor(private httpClient: HttpClient) { }

  private USER_PROFILE = 'http://localhost:8080/api/user/';

  getUser(email:string):Observable<UserProfile>{
    return this.httpClient.get<UserProfile>(this.USER_PROFILE +'userbyemail/'+ email);
  }
}

我不知道 Ngxs,我知道 Ngrx 但我认为你可以尝试这样做:

this.store.dispatch(new GetUser({ email: userEmail }));

替换

payload: { email : string }

payload: string

目前正在等待您做 GetUser({email: userEmail}) 使用该语法