Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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 在以编程方式设置BehaviorSubject之前,BehaviorSubject始终默认为false_Angular_Typescript_Ionic Framework_Rxjs - Fatal编程技术网

Angular 在以编程方式设置BehaviorSubject之前,BehaviorSubject始终默认为false

Angular 在以编程方式设置BehaviorSubject之前,BehaviorSubject始终默认为false,angular,typescript,ionic-framework,rxjs,Angular,Typescript,Ionic Framework,Rxjs,我正在我的应用程序中使用BehaviorSubject进行身份验证,当它设置为true时,意味着用户使用令牌和有效到期日登录,应用程序应将其重定向到主仪表板页面。如果为false,则应提示他们登录 除应用程序恢复和用户登录外,其他一切都正常工作。即使用户已登录且BehaviorSubject设置为true,在再次检查令牌并将其设置回true之前,它也会短暂设置为false。这将导致简短导航到/intro,然后立即正确导航到/users/dashboard 有什么方法可以防止这种故障导航吗 aut

我正在我的应用程序中使用BehaviorSubject进行身份验证,当它设置为true时,意味着用户使用令牌和有效到期日登录,应用程序应将其重定向到主仪表板页面。如果为false,则应提示他们登录

除应用程序恢复和用户登录外,其他一切都正常工作。即使用户已登录且
BehaviorSubject
设置为true,在再次检查令牌并将其设置回true之前,它也会短暂设置为false。这将导致简短导航到
/intro
,然后立即正确导航到
/users/dashboard

有什么方法可以防止这种故障导航吗

authentication.service.ts

export class AuthenticationService {
  state = new BehaviorSubject(false);

  constructor(
    private storage: Storage,
    private platform: Platform
  ) {
    this.platform.ready().then(() => {
      this.checkToken();
    });
  }

  /**
   * Ensures the user has access and is not passed their expiration
   */
  checkToken() {
    this.storage.get('token').then(token => {
      if (token) {
        this.storage.get('expiration').then(expiration => {
          const currentDate = moment.utc().format('YYYY-MM-DD HH:mm:ss');
          if (moment(currentDate).isBefore(expiration)) {
            this.state.next(true);
          }
        });
      }
    });
  }
}
app.component.ts

// Authentication
this.auth.state.subscribe(state => {
  alert(state);
  if (state) {
    this.menuCtrl.enable(true);
    this.router.navigate(['users', 'dashboard']);
  } else {
    this.menuCtrl.enable(false);
    this.router.navigate(['intro']);
  }
});

您可以通过管道
debounceTime
来设置一个时间范围,该时间范围只会发出最新的值。由于这种变化可能会在一瞬间发生,所以在您的情况下,100毫秒可能太长了。考虑根据你的需要降低时间:

this.auth.state.pipe(debounceTime(100)).subscribe(state => {
  alert(state);
  if (state) {
    this.menuCtrl.enable(true);
    this.router.navigate(['users', 'dashboard']);
  } else {
    this.menuCtrl.enable(false);
    this.router.navigate(['intro']);
  }
});
可以说,在去抖动上节省一点时间的解决方案是使用只对
false
值起作用的条件去抖动:

debounce(state => timer(state ? 0 : 100))

如果您不希望
BehaviorSubject
具有默认的
false
值,您可以使用
ReplaySubject(1)
来代替它,它在您显式调用
next()
之前不会发出。@martin false表示用户已注销,这应该是默认值。所以您希望它有一个默认值,而不是同时有一个默认值。从我刚才运行的快速测试来看,这似乎起作用了。@JoeScotto,这很好。我做了一点编辑,以显示一个在您的情况下效果更好的替代方案。有条件地设置去抖动时间可以节省发出
true
时用作去抖动时间的时间。对于我的例子中使用的去盎司时间,这是100毫秒。是的,我使用了50毫秒的条件去盎司。。。似乎工作得很好。谢谢