要解决“Angular的Subject没有在我的路由模块中订阅”的问题,你可以按照以下步骤进行操作:
Subject
和其他必需的依赖项。确保你已经安装了rxjs
库,以便使用Subject
。import { Subject } from 'rxjs';
Subject
实例,并在需要订阅的地方进行订阅。这可以是在路由模块的构造函数中或在其他需要的地方。export class YourRoutingModule {
private mySubject: Subject = new Subject();
constructor() {
this.mySubject.subscribe(data => {
// 处理订阅的数据
});
}
}
Subject
。这可以通过调用next()
方法来完成。export class YourComponent {
constructor(private router: Router) {}
sendDataToSubject() {
this.router.routeReuseStrategy.shouldReuseRoute = () => false;
this.mySubject.next(yourData);
}
}
在这个示例中,我们使用router.routeReuseStrategy.shouldReuseRoute = () => false;
来确保每次导航时都会重新创建组件实例,以便Subject
能够在每个实例中重新进行订阅。
请注意,记得替换YourRoutingModule
、YourComponent
和yourData
为你自己的模块、组件和数据。
这样做后,你就可以在你的路由模块中成功订阅并处理Subject
的数据了。