以下是关于我的项目的一些信息:
- 我正在获取帖子数据用户@ngrx商店和效果。
- 帖子模型有
userId
。 - 当我渲染帖子时,我不想显示
post.data
和post.user.name
有没有办法通过帖子急切加载用户数据?
这个问题的最佳解决方案是什么?
post.model
看起来是这样的:
import { User } from './user.model';
export class Post {
userId: number;
id: number;
title: string;
body: string;
user?: User;
}
我没有得到带有帖子项目的整个用户对象。我只得到用户ID。
编辑:这就是我获取和呈现帖子的方式:
后期效果.ts
@Injectable()
export class PostEffects {
@Effect()
posts$: Observable<Action> = this.actions$
.ofType(postActions.LOAD_LIST)
.debounceTime(300)
.startWith(new postActions.LoadListAction())
.switchMap(() => {
return this.postsService.all()
.map((posts: Post[]) => new postActions.LoadListSuccessAction(posts))
.catch(error => of(new postActions.LoadListFailAction(error)));
});
constructor(
private actions$: Actions,
private postsService: PostsService
) { }
}
post.component.ts
export class PostsComponent {
posts$: Observable<Post[]>;
loaded$: Observable<boolean>;
constructor(private store: Store<State>) {
this.posts$ = store.select(rootReducers.getPostItems);
this.loaded$ = store.select(rootReducers.getPostLoaded);
}
}
post.component.html
<div class="content">
<app-post-list [posts]="posts$ | async"></app-post-list>
</div>
<!-- /.content -->
编辑2:发布reducer文件内容
post.reducer.ts
export function reducer(state = initialState, {type, payload}: postActions.Actions): State {
switch (type) {
case postActions.LOAD_LIST: {
return Object.assign({}, state, {
loading: true
});
}
case postActions.LOAD_LIST_SUCCESS: {
return {
loaded: true,
loading: false,
items: payload,
selectedItem: state.selectedItem
};
}
default: {
return state;
}
}
}