首次实施 NgRx Store 时出现的问题

发布于 2025-01-13 03:56:14 字数 2614 浏览 4 评论 0原文

这是我第一次尝试从头开始实现商店。 此时我有效果、减速器、动作、索引并在应用程序模块中注册了所有内容。目前我想订阅我的商店。 我尝试用本教程。 目前我有错误: store.pipe(select('user')).subscribe(data => (this.players = data.players));

这是 main- 的一部分page.component.ts:

import { Component, OnInit } from '@angular/core';
import { AuthService } from '../../services/auth.service';
import { Router } from '@angular/router';
import { Player } from '../../model/Player';
import { select, Store } from '@ngrx/store';
import { GetPlayersAction } from 'src/app/store/user/actions';

@Component({
  selector: 'app-main-page',
  templateUrl: './main-page.component.html',
  styleUrls: ['./main-page.component.scss']
})
export class MainPageComponent implements OnInit {

  constructor(
    private authService: AuthService,
    private router: Router,
    private store: Store<{ players: Player[]}>
    ) { 
      store.pipe(select('user')).subscribe(data => (this.players = data.players));
    }

  players: Player[] = [];

  currentPlayer: Player;

  ngOnInit(): void {
    this.store.dispatch(new GetPlayersAction());
  }
  


  logout() {
    this.authService.logout();
  }

  showUserProfile() {
    // TODO get logged in user and pass to profile view
    const currentUser: Player = {
      isCurrentUser: true
    }
    this.router.navigateByUrl('/', {skipLocationChange: true}).then(()=> this.router.navigate(['/profile', currentUser],{skipLocationChange: true}));
  }

}

目前我有这样的错误:

错误:src/app/components/main-page/main-page.component.ts:22:25 - 错误 TS2769:没有重载与此调用匹配。过载 1 个(共 9 个), '(mapFn: (状态: { 玩家: Player[]; }) => 未知): (来源$: Observable<{ 玩家: 玩家[]; }>) =>可观察到的”,给出了 以下错误。 “string”类型的参数不可分配给“(state: {players: Player[]; }) =>”类型的参数未知'。超载 9 个中的 2 个, '(key: "玩家"): (source$: Observable<{ 玩家: Player[]; }>) =>; Observable',给出了以下错误。 “用户”类型的参数不可分配给“玩家”类型的参数。

错误:src/app/components/main-page/main-page.component.ts:22:73 - 错误 TS2339:类型“未知”上不存在属性“玩家”。

This is my first attempt to implement store from the scratch.
At this moment i have effect, reducer, action, index and registred everything in app module. Currently i want to subscripe to my store.
I tried to do it with this tutorial.
Currently i have error in line: store.pipe(select('user')).subscribe(data => (this.players = data.players));

This is a part of main-page.component.ts:

import { Component, OnInit } from '@angular/core';
import { AuthService } from '../../services/auth.service';
import { Router } from '@angular/router';
import { Player } from '../../model/Player';
import { select, Store } from '@ngrx/store';
import { GetPlayersAction } from 'src/app/store/user/actions';

@Component({
  selector: 'app-main-page',
  templateUrl: './main-page.component.html',
  styleUrls: ['./main-page.component.scss']
})
export class MainPageComponent implements OnInit {

  constructor(
    private authService: AuthService,
    private router: Router,
    private store: Store<{ players: Player[]}>
    ) { 
      store.pipe(select('user')).subscribe(data => (this.players = data.players));
    }

  players: Player[] = [];

  currentPlayer: Player;

  ngOnInit(): void {
    this.store.dispatch(new GetPlayersAction());
  }
  


  logout() {
    this.authService.logout();
  }

  showUserProfile() {
    // TODO get logged in user and pass to profile view
    const currentUser: Player = {
      isCurrentUser: true
    }
    this.router.navigateByUrl('/', {skipLocationChange: true}).then(()=> this.router.navigate(['/profile', currentUser],{skipLocationChange: true}));
  }

}

Currently i have error like this:

Error: src/app/components/main-page/main-page.component.ts:22:25 -
error TS2769: No overload matches this call. Overload 1 of 9,
'(mapFn: (state: { players: Player[]; }) => unknown): (source$:
Observable<{ players: Player[]; }>) => Observable', gave the
following error.
Argument of type 'string' is not assignable to parameter of type '(state: { players: Player[]; }) => unknown'. Overload 2 of 9,
'(key: "players"): (source$: Observable<{ players: Player[]; }>) =>
Observable<Player[]>', gave the following error.
Argument of type '"user"' is not assignable to parameter of type '"players"'.

Error: src/app/components/main-page/main-page.component.ts:22:73 -
error TS2339: Property 'players' does not exist on type 'unknown'.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文