首次实施 NgRx Store 时出现的问题
这是我第一次尝试从头开始实现商店。 此时我有效果、减速器、动作、索引并在应用程序模块中注册了所有内容。目前我想订阅我的商店。 我尝试用本教程。 目前我有错误: 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论