首页 > 解决方案 > angular 11 不在订阅内导航

问题描述

问题是我无法重定向(URL 更改内容没有)

export class HomePageComponent implements OnInit {

  constructor(private authService: MsalService, private activatedRoute: ActivatedRoute,
    private router: Router) { }

  ngOnInit(): void {
    const self = this;
    this.activatedRoute.queryParams.subscribe(params => {
      if (this.authService.instance.getActiveAccount() != null) {
        let redirectUrl = params['redirectUrl'];
        if(redirectUrl !== undefined) {
          self.router.navigateByUrl('/activate');
          return true; 
        }
      }
    });
  }

}

但是这段代码有效,为什么?router.navigateByUrl('/activate') 不在订阅范围内

ngOnInit(): void {
    const self = this;
    
    this.router.navigateByUrl('/activate');
    this.activatedRoute.queryParams.subscribe(params => {
      if (this.authService.instance.getActiveAccount() != null) {
        let redirectUrl = params['redirectUrl'];
        if(redirectUrl !== undefined) {
          self.router.navigateByUrl('/activate');
          return true; 
        }
      }
    });
  }

我真的需要 redirectUrl 而不是现在硬编码

标签: angularangular-routermsal.js

解决方案


推荐阅读