Ngrx 商店收到错误,因为类型上不存在属性“ofType”

Posted

技术标签:

【中文标题】Ngrx 商店收到错误,因为类型上不存在属性“ofType”【英文标题】:Ngrx store getting error as Property 'ofType' does not exist on type 【发布时间】:2019-06-11 21:36:50 【问题描述】:

我正在尝试使用 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 ''.

打字稿文件:

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"
  

任何人在这里帮助我....提前谢谢。

【问题讨论】:

【参考方案1】:

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

所以,而不是:

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

做:

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

【讨论】:

答案在 2020 年仍然适用! [角8 | ngrx-8 ] 在 NgRx 6.1 中,ofType 函数被标记为弃用,取而代之的是 ofType 运算符,在 NgRx v7 中,这个函数被删除了。请参阅ngrx.io/guide/migration/v7 关于删除 ofType 实例运算符的信息。

以上是关于Ngrx 商店收到错误,因为类型上不存在属性“ofType”的主要内容,如果未能解决你的问题,请参考以下文章

升级@ngrx/Store 时,类型“操作”上不存在属性“有效负载”

实现 ngRx 时出错“类型 'Actions<Action>' 上不存在属性 'pipe'。”

“错误”类型上不存在属性“代码”

为啥我会收到此错误? 'Observable<boolean>' 类型上不存在属性 'map'

类型“文档<any>”上不存在属性“密码”

类型“”上不存在属性“forEach”