我正在使用ngrx/store像下面那樣測試狀態管理功能。非常奇怪的是我不能在user.selectors.ts中使用select方法。它表示Property'select'在類型'Observable'上不存在。 AppState在reducer.ts中正確定義。爲了使用'select',在user.selectors.ts中添加了'@ ngrx/core/add/operator/select',並且我確認了位於node_modules文件夾中的select.ts文件。無法在ngrx/store中使用select
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);
}
能否請你幫我這個?
我建立的方式如下。
app - auth
- core - store
- service
- shared
裏面存放的目錄,5個文件是居民,user.actions.ts,user.reducer.ts,user.selectors.ts,APP-store.ts和index.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看起來像下面。
import { User } from './user.reducer';
export interface AppState {
user: User;
};
顯示您的模塊代碼是最重要的,你如何使用它 – Skeptor
我增加了定義模塊的代碼的行動不應該注射,不知道,幫我。 –