在Angular 11中,无法在subscribe中导航,因为该过程在异步执行中发生,导致Angular不会检测到更改。为了解决此问题,可以使用Router的navigate方法来导航。下面是代码示例:
import { Router } from '@angular/router';
export class SomeComponent { constructor(private router: Router) {}
someFunction() { // 使用Router的navigate方法导航 this.someService.someMethod().subscribe(() => { this.router.navigate(['/some-path']); }); } } 在上述示例中,当subscribe中的异步操作完成后,使用Router的navigate方法导航到指定路径。这样就可以解决Angular 11无法在subscribe中导航的问题。