gpt4 book ai didi

Ngrx 存储出现错误,因为类型上不存在属性 'ofType'

转载 作者:行者123 更新时间:2023-12-02 00:59:02 28 4
gpt4 key购买 nike

我正在尝试使用 ngrx/store 开发应用程序,但出现错误。无法弄清楚问题。有人帮帮我吗?

这是我的错误:

ERROR in src/app/store/effects/authentication.effects.ts(25,7): error TS2339: Property 'ofType' does not exist on type 'Actions<Action>'.
src/app/store/effects/authentication.effects.ts(29,59): error TS2339: Property 'email' does not exist on type '{}'.
src/app/store/effects/authentication.effects.ts(29,74): error TS2339: Property 'password' does not exist on type '{}'.
src/app/store/effects/authentication.effects.ts(33,73): error TS2339: Property 'email' does not exist on type '{}'.

typescript 文件:

import { Injectable } from '@angular/core';
import { Action } from '@ngrx/store';
import { Router } from '@angular/router';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { Observable, of } from 'rxjs';
import { map, switchMap, catchError, tap } from 'rxjs/operators';

import { AuthenticationService } from './../../service/authentication.service';
import { AuthenticationActionTypes,
Login, LoginSuccess,
LoginFailure, Logout } from './../actions/authentication.actions';

@Injectable({
providedIn: 'root'
})
export class AuthenticationEffects {

constructor(
private actions:Actions,
private authenticationService:AuthenticationService,
private router:Router) {}

@Effect()
Login: Observable<any> = this.actions
.ofType(AuthenticationActionTypes.LOGIN)
.pipe(
map((action: Login) => action.payload),
switchMap(payload => {
return this.authenticationService.login(payload.email, payload.password)
.pipe(
map((user) => {
console.log(user);
return new LoginSuccess({token: user.token, email: payload.email});
}),
catchError((error) => {
return of(new LoginFailure({ error: error }));
}));
}));

@Effect({ dispatch: false })
LoginSuccess: Observable<any> = this.actions.pipe(
ofType(AuthenticationActionTypes.LOGIN_SUCCESS),
tap((user) => {
localStorage.setItem('token', user.payload.token);
localStorage.setItem('email', user.payload.email);
this.router.navigateByUrl('/');
})
);

@Effect({ dispatch: false })
LoginFailure: Observable<any> = this.actions.pipe(
ofType(AuthenticationActionTypes.LOGIN_FAILURE)
);

@Effect({ dispatch: false })
public Logout: Observable<any> = this.actions.pipe(
ofType(AuthenticationActionTypes.LOGOUT),
tap((user) => {
localStorage.removeItem('token');
localStorage.removeItem('email');
this.router.navigateByUrl('/login');
})
);

}

JSON 文件:

{
"name": "authentication",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "~7.2.0",
"@angular/common": "~7.2.0",
"@angular/compiler": "~7.2.0",
"@angular/core": "~7.2.0",
"@angular/forms": "~7.2.0",
"@angular/platform-browser": "~7.2.0",
"@angular/platform-browser-dynamic": "~7.2.0",
"@angular/router": "~7.2.0",
"@ngrx/core": "^1.2.0",
"@ngrx/effects": "^7.0.0",
"@ngrx/store": "^7.0.0",
"core-js": "^2.5.4",
"rxjs": "~6.3.3",
"tslib": "^1.9.0",
"zone.js": "~0.8.26"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.12.0",
"@angular/cli": "~7.2.2",
"@angular/compiler-cli": "~7.2.0",
"@angular/language-service": "~7.2.0",
"@types/node": "~8.9.4",
"@types/jasmine": "~2.8.8",
"@types/jasminewd2": "~2.0.3",
"codelyzer": "~4.5.0",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~3.1.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~1.1.2",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.11.0",
"typescript": "~3.2.2"
}
}

任何人都可以帮助我......提前致谢。

最佳答案

在第一个效果中,您没有将 ofType 放入 pipe 中:

所以,而不是:

Login: Observable<any> = this.actions
.ofType(AuthenticationActionTypes.LOGIN)
.pipe(

做:

Login: Observable<any> = this.actions.pipe(
ofType(AuthenticationActionTypes.LOGIN)

关于Ngrx 存储出现错误,因为类型上不存在属性 'ofType',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54240118/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com