标签: ngrx

使用 ngrx 存储进行身份验证的路由防护

我正在尝试创建一个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。预先感谢您的任何帮助。

rxjs ngrx angular angular-route-guards

2
推荐指数
1
解决办法
3547
查看次数

如何从 ngrx 商店获取价值

我有一个 Angular 8 项目,我用来ngrx在状态存储中存储对象数组。我设法做到了这一点,但现在,我想使用post request状态存储中的数据创建服务器。

this.dataService.postData(this.store.pipe(select('dataStore')));
Run Code Online (Sandbox Code Playgroud)

这是我正在尝试的代码,但它不起作用。谢谢

ngrx angular

2
推荐指数
1
解决办法
5396
查看次数

初始化存储时不会触发 ROOT_EFFECTS_INIT

我有一个带有 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 的存储库,但我应用的解决方案没有触发我的功能。

难道我做错了什么 ?

redux ngrx

2
推荐指数
1
解决办法
2083
查看次数

storeFreeze 和 ngrx8

我刚刚升级到 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)

ngrx angular ngrx-store angular8

2
推荐指数
1
解决办法
2553
查看次数

选择器 NGRX 内的调度操作

在我的选择器中,我正在检查存储中的数据是否已加载并对应于路由器参数。路由器是“事实来源”,因此如果未加载数据,我想发送一个操作来获取它。在选择器中做这样的事情可以吗?

  (currentGameState, router): Game => {
     if (currentGameState.game.id === router.state.params.gameId && 
         currentGameState.isLoaded) {
         return currentGameState.game;
     }
  }
Run Code Online (Sandbox Code Playgroud)

rxjs typescript redux ngrx angular

2
推荐指数
1
解决办法
3372
查看次数

如何将 NgRx 8 操作与 NgRx 7 减速器一起使用

我正在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)

typescript ngrx angular ngrx-store angular8

2
推荐指数
1
解决办法
929
查看次数

Angular + Ngrx:在组件和函数中选择值的最佳实践

我有与ngrx get value in function几乎相同的问题,但我想知道答案(请参阅该主题中的评论)是否仍然是当今的最佳实践。

我的情况是:

  1. 我想从商店获取 2 个值以显示在我的组件中 --> 我使用选择器和异步管道
  2. 我需要将这些相同的值传递给 Angular Material 对话框,以便在对话框组件中使用它们。

我有一个可行的解决方案:我在 ngOnInit() 中使用订阅函数,并使用 Observable 中的值设置一个局部变量。因此,我不再需要使用异步管道,我可以轻松地将值传递给某些函数...这听起来像是最好的选择,但在我寻找答案的任何地方,我都会看到“避免使用订阅”。

所以我想知道:

  • 有没有更好的方法来处理这种情况?
  • 或者这是“处理这种情况的 ngrx/rxjs 方式”?
  • 或者我是否使用选择器 Observable 和异步管道来显示组件中的值并订阅 Observable 以创建一个局部变量来传递给我的函数(这似乎有点多余......)
  • 或者 ...?

我的问题是我想要一种一致的方法来处理 ngrx 值,但现在看起来我需要根据情况使用两种方法:

  • 用于显示组件中的值的异步管道(社区的首选方法)
  • 在本地函数中使用值时的订阅(社区建议“除非必要”不要使用订阅)

(令我惊讶的是:很难在互联网上找到明确的答案......)

redux ngrx angular angular-ngrx-data rxjs-observables

2
推荐指数
1
解决办法
2203
查看次数

如何修复: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)

redux ngrx angular

2
推荐指数
1
解决办法
6972
查看次数

在 mat-selection-list 中实现搜索功能

我一直在寻找解决方案,但无法弄清楚应该如何制作。这是我几乎要寻找的内容的堆栈闪电战,但我不希望它看起来像下拉列表。 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)

angular-material ngrx angular

2
推荐指数
1
解决办法
4787
查看次数

循环中的 NgRx-Entity-Selector

我有以下问题。假设我向服务器发出请求,服务器按特定顺序提供了 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 函数的解决方案,但是模板变得非常难看。我想这一定是一个普遍的问题。有人对此有一个巧妙的解决方案吗?

谢谢你!

ngrx angular ngrx-store ngrx-entity

2
推荐指数
1
解决办法
1376
查看次数