我正在尝试创建一个AuthGuard来检查用户是否可以访问路由,否则重定向到登录视图。我想Observable<Boolean|UrlTree>从该
canActivate方法返回一个。这是我到目前为止所拥有的。
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
return this.store$.select(appState => appState.auth.authUser)
.pipe(map(authUser => Boolean(authUser)));
}
Run Code Online (Sandbox Code Playgroud)
但是,我不太确定如何/在哪里可以从可观察到重定向到 UrlTree /login,因为我对这整个事情很陌生,特别是 rxjs。预先感谢您的任何帮助。
我有一个 Angular 8 项目,我用来ngrx在状态存储中存储对象数组。我设法做到了这一点,但现在,我想使用post request状态存储中的数据创建服务器。
this.dataService.postData(this.store.pipe(select('dataStore')));
Run Code Online (Sandbox Code Playgroud)
这是我正在尝试的代码,但它不起作用。谢谢
我有一个带有 Init Effect 的效果 =>
@Effect()
init$ = this.actions$.pipe(
ofType(ROOT_EFFECTS_INIT),
map(action =>
new featureActions.GetAllMissionRequest({
projectID: environment.projectId,
projectContextID: environment.projectId
})
)
);
Run Code Online (Sandbox Code Playgroud)
我在文件底部添加了这个,我还尝试包含一个延迟,但是当我的商店初始化时,这个效果永远不会触发。
我检查了 ngrx 的存储库,但我应用的解决方案没有触发我的功能。
难道我做错了什么 ?
我刚刚升级到 ngrx/store 版本 8。我注意到 ng 更新已经删除了所有 storeFreeze 的出现。也将其从metaReducer 中删除。
所以我的问题是 -为什么?
在 ngrx 8 中使用 storeFreeze 是否有问题?
在 ngrx8 之前:
import { ActionReducerMap, MetaReducer } from '@ngrx/store';
import { storeFreeze } from 'ngrx-store-freeze';
import * as fromGroupMember from './group-member.reducer';
import * as fromDirectoryForm from './directory-filter-form.reducer';
export const metaReducers: MetaReducer<IState>[] =
(localStorage && localStorage.getItem('production') === 'false') ? [storeFreeze] : [];
Run Code Online (Sandbox Code Playgroud)
后:
import { ActionReducerMap, MetaReducer } from '@ngrx/store';
import * as fromGroupMember from './group-member.reducer';
import * as fromDirectoryForm from …Run Code Online (Sandbox Code Playgroud) 在我的选择器中,我正在检查存储中的数据是否已加载并对应于路由器参数。路由器是“事实来源”,因此如果未加载数据,我想发送一个操作来获取它。在选择器中做这样的事情可以吗?
(currentGameState, router): Game => {
if (currentGameState.game.id === router.state.params.gameId &&
currentGameState.isLoaded) {
return currentGameState.game;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在NgRx我的新 Angular 8 项目中使用该库。我被告知他们使用createActionie创建操作NgRx 8,但他们使用NgRx 7. 我接到了一项任务,其中我使用了NgRx 8我的减速器,现在我必须将其更改为NgRx 7。我的动作和减速器如下:
书本.actions.ts
import { createAction, props } from "@ngrx/store";
import { Book } from "./book";
export const BeginGetBooksAction = createAction("BeginGetBooks");
export const SuccessGetBooksAction = createAction(
"SuccessGetBooks",
props<{ payload: Book[] }>()
);
export const BeginAddBookAction = createAction(
"BeginAddBook",
props<{ payload: Book }>()
);
export const SuccessAddBookAction = createAction(
"SuccessAddBook",
props<{ payload: Book[] }>()
);
Run Code Online (Sandbox Code Playgroud)
书本.reducer.ts
import { Action, createReducer, on } …Run Code Online (Sandbox Code Playgroud) 我有与ngrx get value in function几乎相同的问题,但我想知道答案(请参阅该主题中的评论)是否仍然是当今的最佳实践。
我的情况是:
我有一个可行的解决方案:我在 ngOnInit() 中使用订阅函数,并使用 Observable 中的值设置一个局部变量。因此,我不再需要使用异步管道,我可以轻松地将值传递给某些函数...这听起来像是最好的选择,但在我寻找答案的任何地方,我都会看到“避免使用订阅”。
所以我想知道:
我的问题是我想要一种一致的方法来处理 ngrx 值,但现在看起来我需要根据情况使用两种方法:
(令我惊讶的是:很难在互联网上找到明确的答案......)
如何修复:ngRx 中的“类型错误:实体不可迭代”:如果有人明白这一点,请告诉我。
//用户.action
import { Course } from '../model/user';
import { createAction, props } from '@ngrx/store';
import { Update } from '@ngrx/entity';
export const loadCourses = createAction(
'[Courses List] Load Courses via Service'
);
export const coursesLoaded = createAction(
'[Courses Effect] Courses Loaded Successfully',
props<{ courses: Course[] }>()
);
export const createCourse = createAction(
'[Create Course Component] Create Course',
props<{ course: Course }>()
);
export const deleteCourse = createAction(
'[Courses List Operations] Delete Course',
props<{ courseId: string }>()
);
export …Run Code Online (Sandbox Code Playgroud) 我一直在寻找解决方案,但无法弄清楚应该如何制作。这是我几乎要寻找的内容的堆栈闪电战,但我不希望它看起来像下拉列表。 https://stackblitz.com/edit/angular-yo2o9o?file=app%2Fapp.component.html
我希望有一个像下面我自己的代码一样的列表,但在列表上实现一个搜索字段,并使用它根据输入过滤 users$ (但仍然保存所选用户)...
编辑:找到这个,https://stackblitz.com/edit/angular-i3pfu2-xgembc ?file=app%2Flist-selection-example.ts ,
这就是它应该看起来的样子,但我无法让它与我的代码一起使用。
网页:
<div class="userContainer">
<p>Choose participants</p>
<mat-selection-list class="form-group" #selectedUsers formControlName="users">
<mat-list-option *ngFor="let user of users$ | async" [value]="user">
{{ user.name }}
</mat-list-option>
</mat-selection-list>
<p>Invited users: {{ selectedUsers.selectedOptions.selected.length }}</p>
</div>
Run Code Online (Sandbox Code Playgroud)
users$ 来自我所在州的选择器
this.store$.dispatch(new fromUsers.GetUsers());
this.users$ = this.store$.pipe(select(fromUsers.getRelevantUsers(+this.userId)));
Run Code Online (Sandbox Code Playgroud)
这就是我正在使用的表格..
createEventForm() {
this.eventForm = this.fb.group(
{
users: [null],
more inside...
}
);
}
Run Code Online (Sandbox Code Playgroud) 我有以下问题。假设我向服务器发出请求,服务器按特定顺序提供了 id 列表。其他调用正在加载实体,我将它们置于单独的状态:
所以简化后我的状态类似于:
{
topList: [ 7, 3, 1, 4 ],
entities: { // basic ngrx-entity structure
ids: ...
entities: ...
}
}
Run Code Online (Sandbox Code Playgroud)
该组件的简化版本如下所示。
@Component({
selector: 'my-list-component',
template: `
<div *ngFor="let id in (toplist$ | async)">
<my-single-item-component [entity]="???"></my-single-item-component>
</div>
`
})
export class MyComponent implements OnInit {
public toplist$ = this.store.pipe(select(getToplist));
public getEntityById$ = this.store.pipe(???)
constructor(store: Store<State>) {}
ngOnInit() { }
}
Run Code Online (Sandbox Code Playgroud)
所以我需要从模板中调用一些带有动态 id 的选择器。
我找到了一个带有 Observable 函数的解决方案,但是模板变得非常难看。我想这一定是一个普遍的问题。有人对此有一个巧妙的解决方案吗?
谢谢你!