接下来如何解决错误不是角度功能

时间:2020-03-16 10:57:13

标签: javascript angular typescript

这是代码:

async ngOnInit() {
    @Select(UserPageState.get('collection')) users$: BehaviorSubject<Array<Partial<User>>>;


    const USER = this.creds.credentials['id'];
    this.users$.subscribe(param => this.users$.next(param.filter(x => x.id !== USER)));

    await this.store.dispatch(new UserPageList({ start: 1, length: this.pageSize })).toPromise();
    }

错误

ERROR TypeError: _this.users$.next is not a function
    at SafeSubscriber._next

user.ts

  username: string;
  password?: string;
  token: string;
  avatar: string;
  firstName: string;
  lastName: string;
  email: string;
  department: string;
  position: string;
  role: string;
  status: string;
  enabled: boolean;
  groups: Array<Group>;
  location: Array<Sbu>;

每次运行应用程序时,错误始终是下一个错误,而不是函数。 我试图将行为主题更改为BehaviorSubject(Array>);

我的所有代码都是错误的。

2 个答案:

答案 0 :(得分:2)

似乎您正在使用Ngxs库进行状态管理,并使用其Select装饰器来检索状态切片。

@Select返回简单的observable,而不返回BehaviorSubject。 您的声明应为:

@Select(UserPageState.get('collection')) users$: Observable<User[]>;

或使用Partial<User>[],但我没有您的实施细节

因此,如果您只想过滤此选择器检索到的用户:

export class YourComponent {
  @Select(UserPageState.get('collection')) users$: Observable<User[]>;
  filteredUsers$: Observable<User[]>;

  ngOnInit() {
    const USER = this.creds.credentials['id'];
    this.filteredUsers = this.users$.pipe(
      map(users => users.filter(user => user.id !== USER))
    );

    this.store.dispatch(new UserPageList({ start: 1, length: this.pageSize }));
  }
}

在您的模板中:

  <ul>
    <li *ngFor="let user of filteredUsers$ | async">
      {{ user.name }}
    </li>
  </ul>

请注意,您可能应该考虑创建一个专用选择器,并在商店中也添加USER

答案 1 :(得分:0)

尝试这样。可能是工作 从'rxjs'导入{of,Subject,Observable};

  private users$= new Subject<void>();
 onusers$: Observable<void>;


 this.onusers$ = this.users$.asObservable();


   this.users$.next(param:{param.filter(x => x.id !== USER))});