Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 在rxjs中,如何取消订阅;“内部”&引用;间隔;使用takeUntil或takeWhile进行观察_Angular_Rxjs - Fatal编程技术网

Angular 在rxjs中,如何取消订阅;“内部”&引用;间隔;使用takeUntil或takeWhile进行观察

Angular 在rxjs中,如何取消订阅;“内部”&引用;间隔;使用takeUntil或takeWhile进行观察,angular,rxjs,Angular,Rxjs,[EDIT 11/5]在使用一些建议的解决方案进行测试后,我注意到我的一些rxjs导入错误(我是从“rxjs”导入的“rxjs/internal”iso)。所以如果你遇到这样的错误,你可以看看 关于Stackoverflow,已经存在一些与取消订阅observable相关的问题,尽管这些问题对我的具体问题都没有很大帮助 在我的(“聊天”)应用程序中,我可以通过在搜索框中键入用户姓名来搜索用户。获取用户后,我想显示这些用户是否在线 这是我的方法 有一个用于捕获搜索词的行为主题: private t

[EDIT 11/5]在使用一些建议的解决方案进行测试后,我注意到我的一些rxjs导入错误(我是从“rxjs”导入的“rxjs/internal”iso)。所以如果你遇到这样的错误,你可以看看

关于Stackoverflow,已经存在一些与取消订阅observable相关的问题,尽管这些问题对我的具体问题都没有很大帮助

在我的(“聊天”)应用程序中,我可以通过在搜索框中键入用户姓名来搜索用户。获取用户后,我想显示这些用户是否在线

这是我的方法

有一个用于捕获搜索词的行为主题:

private term$ = new BehaviorSubject(null);
只要$observable这个术语有了新的价值,我就开始寻找新用户:

users$: Observable<User[]> = this.term$.pipe(
    filter(term => !!term),
    switchMap(term => this.usersService.searchUsers(term))
);
现在的问题是,只要有一个新的搜索项(在$observable项上),就应该取消订阅“checkloggedininterval”observable项。我曾尝试在“checkloggedininterval”可观察对象和父“users$”可观察对象中使用takeUntil操作符,但没有效果。使用takeWhile操作符也是无效的。

尝试在调用
checkLoggedInF
的位置之前使用
.tap()
,并将
checkLoggedInF
中的
可观察的
保存在属性中

checkLoggedInF = user => {
    this.checkLoggedInF$ = interval(5000).pipe(
      switchMap(() => this.usersService.isLoggedIn(user.loginnaam).pipe(
        map(loggedIn => ({...user, loggedIn}))
      ))
    );
    return this.checkLoggedInF$;
}

users$: Observable<any[]> = this.term$.pipe(
    filter(term => !!term),
    switchMap(term => this.usersService.searchUsers(term)),
    tap(() => { this.checkLoggedInF$ && this.checkLoggedInF$.unsubscribe() })
    switchMap(users => zip(...users.map(user => this.checkLoggedInF(user))))
);

最后我犯了一个愚蠢的错误,我的一些“rxjs导入”是错误的(我是从rxjs/internal导入的)。当我修复导入时,switchMap按预期工作(在“上游”更改时关闭订阅)


因此,简而言之:不要从“rxjs/internal”导入

直到
应该做你想做的事情。我写了一个用户和操作员来做这类事情:您可能还想阅读文件顶部评论中引用的Strongbrew文章。谢谢您的回答。最后我犯了一个愚蠢的错误,我的一些rxjs导入是错误的(从rxjs/internal导入)。当我修复导入时,switchMap按预期工作(在“上游”更改时关闭订阅)。
checkLoggedInF = user => {
    return interval(5000).pipe(
      switchMap(() => this.usersService.isLoggedIn(user.loginnaam).pipe(
        map(loggedIn => ({...user, loggedIn}))
      ))
    );
}
checkLoggedInF = user => {
    this.checkLoggedInF$ = interval(5000).pipe(
      switchMap(() => this.usersService.isLoggedIn(user.loginnaam).pipe(
        map(loggedIn => ({...user, loggedIn}))
      ))
    );
    return this.checkLoggedInF$;
}

users$: Observable<any[]> = this.term$.pipe(
    filter(term => !!term),
    switchMap(term => this.usersService.searchUsers(term)),
    tap(() => { this.checkLoggedInF$ && this.checkLoggedInF$.unsubscribe() })
    switchMap(users => zip(...users.map(user => this.checkLoggedInF(user))))
);
checkLoggedInF = user => {
   this.checkLoggedInF$ && this.checkLoggedInF$.unsubscribe() 
   this.checkLoggedInF$ = interval(5000).pipe(
     switchMap(() => this.usersService.isLoggedIn(user.loginnaam).pipe(
       map(loggedIn => ({...user, loggedIn}))
     ))
   );
   return this.checkLoggedInF$;
}