Angular 无法在ngrx/存储中使用select

Angular 无法在ngrx/存储中使用select,angular,ngrx,ngrx-store,Angular,Ngrx,Ngrx Store,我正在使用ngrx/store测试状态管理功能,如下所示。非常奇怪的是,我不能在user.selectors.ts中使用select方法。它表示类型“Observable”上不存在属性“select”。 AppState在reducers.ts中正确定义。要使用“选择”,请在user.selectors.ts中添加导入“@ngrx/core/add/operator/select”,我还确认在node_modules文件夹中的正确位置有select.ts文件 user.actions.ts im

我正在使用ngrx/store测试状态管理功能,如下所示。非常奇怪的是,我不能在user.selectors.ts中使用select方法。它表示类型“Observable”上不存在属性“select”。 AppState在reducers.ts中正确定义。要使用“选择”,请在user.selectors.ts中添加导入“@ngrx/core/add/operator/select”,我还确认在node_modules文件夹中的正确位置有select.ts文件

user.actions.ts

import { Injectable } from '@angular/core';
import { Action } from '@ngrx/store';

@Injectable()
export class UserActions {

  static LOGIN = 'LOGIN';
  static LOGOUT = 'LOGOUT';

  logIn (token: string) {
    return { type: UserActions.LOGIN, token };
  }

  logOut() : Action {
    return { type: UserActions.LOGOUT };
  }
}
user.reducers.ts

import '@ngrx/core/add/operator/select';
import { Observable } from 'rxjs/Observable';
import { Action } from '@ngrx/store';
import { UserActions } from './user.actions';

export * from './user.actions';

export interface User {
  access_token: string;
  is_authenticated: boolean;
};

const initialUserState: User = {
  access_token: '',
  is_authenticated: false
};

export function userReducer(state = initialUserState, action: Action): User {
  switch (action.type) {
    case UserActions.LOGIN:
      return Object.assign( {}, state, { access_token: action.payload })
    case UserActions.LOGOUT:
      return Object.assign( {}, state, { access_token:'' } )

    default:
    return state;
  }
};
user.selectors.ts

import { Observable } from 'rxjs/Observable';
import { UserProfile } from './user.reducers';
import { AppState } from '../reducers';
import '@ngrx/core/add/operator/select';

export function getUser$(state$: Observable<AppState>): Observable<User> {
  return state$.select(state => state.user.access_token);
}
在store目录中,有5个文件是常驻文件、user.actions.ts、user.reducer.ts、user.selectors.ts、app-store.ts和index.ts

我试图从存储目录中创建一个模块,如下所示

索引

import { Observable } from 'rxjs/Observable';
import { NgModule } from '@angular/core';
import { StoreModule, combineReducers } from '@ngrx/store';
import { compose } from '@ngrx/core';
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
import 'rxjs/add/operator/let';
import '@ngrx/core/add/operator/select';
import { UserActions } from './user-profile.actions';
import { userReducer } from './user-profile.reducer';
import { AppState } from './app-store';

@NgModule({
  imports: [
    StoreModule.provideStore({userReducer}),
  ],
  declarations: [],
  exports: [],
  providers: [UserActions]
})
export class CoreStoreModule {};
仅供参考,app-store.ts如下所示

app - auth
    - core    - store
              - service
    - shared
import { User } from './user.reducer';

export interface AppState {
  user: User;
};

看到您的语法,看起来您使用的是较旧的ngrx版本2。较新的版本4没有什么不同。我将在这里给出旧版本的语法,因为您已经在使用它了

您的操作代码是正确的,只需删除@Injectable,我们不会在任何地方注入它。减速机看起来也不错

如果您想在用户服务中访问,代码需要如下:

import { Store } from '@ngrx/store';    

@Injectable()
public class UserService{
    constructor(private store: Store<any>){}
    public getUser(): Observable<User>{
       return this.store.select('userReducer');
    }
}

显示最重要的模块代码,操作不应是可注入的,不确定如何使用itI添加了定义模块的代码,请帮助我。您好,我似乎遇到了类似的问题,在问了一个新问题后发现了这个问题。在他的示例中,user.selectors.ts会发生什么情况?在我的例子中,我们在我们的reducer类中有这些方法,我有点迷失了需要移动到哪里。我会尽力帮助你
constructor(private userService: UserService){
    this.userService.getUser().subscribe((user: User)=> console.log(user));
}