Angular 订阅未触发的主题

Angular 订阅未触发的主题,angular,Angular,我正在努力使一个订阅工作没有任何结果 其想法是拥有一个简单的RoutingService,订阅AuthService,因为我希望路由/导航发生在AuthService之外。当AuthService(登录/注销)发生更改时,我希望向RoutingService发送一个事件,并根据结果导航到组件或不导航到组件 在添加代码之前,我已经在其他帖子上做了一些研究。以下是您现在想到的问题的一些答案: 我的所有服务在我的根模块中只声明一次,因此它们是单例的。我没有包含提供商元素的任何其他服务/模块 我试着使

我正在努力使一个订阅工作没有任何结果

其想法是拥有一个简单的RoutingService,订阅AuthService,因为我希望路由/导航发生在AuthService之外。当AuthService(登录/注销)发生更改时,我希望向RoutingService发送一个事件,并根据结果导航到组件或不导航到组件

在添加代码之前,我已经在其他帖子上做了一些研究。以下是您现在想到的问题的一些答案:

  • 我的所有服务在我的根模块中只声明一次,因此它们是单例的。我没有包含提供商元素的任何其他服务/模块
  • 我试着使用主语、重播主语和BheaviourSubject,同样的结果
找到下面的代码:

AuthService类

@Injectable() export class AuthService implements OnInit {

    private authorized = false;
    private googleResponse: GoogleUser;
    authorizationChanges: BehaviorSubject<boolean>;
    authorizationChangesObservable;

    constructor(private router: Router, private googleAuth: GoogleAuthService, private ngZone: NgZone) {
        this.authorizationChanges = new BehaviorSubject<boolean>(this.authorized);
        this.authorizationChangesObservable = this.authorizationChanges.asObservable();
    }

    ngOnInit(): void {
        // also tried to initialize the subject and observable here, same result   }

      //method triggered after a successful signIn   
    private signInSuccessHandler(res: GoogleUser) {
        this.googleResponse = res;
        if (this.validateAccountDomain()) {
            this.authorized = res.isSignedIn();
        }
        console.log('triggering authorizationChanges event');  //<-- I can see this log message in the console
        // So far this is not working
        this.authorizationChanges.next(this.authorized);
    }
}
@Injectable()导出类AuthService实现OnInit{
私人授权=假;
谷歌私人回应:谷歌用户;
授权变更:行为主体;
授权变更可维护;
构造函数(专用路由器:路由器,专用googleAuth:GoogleAuthService,专用ngZone:ngZone){
this.authorizationChanges=新行为主体(this.authorizated);
this.authorizationChangesObservable=this.authorizationChanges.asObservable();
}
ngOnInit():void{
//还尝试初始化主题并在此处观察,结果相同}
//方法在成功登录后触发
私人签名成功处理程序(res:GoogleUser){
this.googleResponse=res;
if(this.validateAccountDomain()){
this.authorized=res.isSignedIn();
}
console.log(‘触发授权更改事件’);//{
console.log(authorized);//,如
OnInit()
可用于指令和组件。它们不适用于其他类型,如您案例中的服务

OnInit与绑定有关。当您希望等待解析输入值时使用它,因为它们在构造函数中不可用。构造函数可以被调用多次。例如,当您更改路由并加载不同的组件时,每次都会“构造”并销毁它们


您可以将ngOnInit逻辑移动到可注入类的构造函数中。

删除
此。授权更改可服务
或使用它,并告诉我们它是如何运行的,忘记清理一点代码,使用了这两种情况,结果相同。控制台不记录,导航也不会触发。让我将其添加到POST,它是否工作正常当您直接从auth服务进行路由时?是的。如果我直接从SignInsAccessHandler方法进行路由,该方法按预期工作,但我不想将路由与AuthServiceWell混合使用,那么您已经有了一个“本机”angular提供的服务。在这里,您只是添加了一个步骤。我知道您想要分离关注点,但有时并不需要!这是一个我不知道的非常有用的信息,感谢您的回复。不幸的是,将init代码移动到构造函数块并不能解决问题
@Injectable()
export class RoutingService implements OnInit, OnDestroy {
    authorizationUpdates: Subscription;
    constructor(private authService: AuthService, private router: Router) { //tried to subscribe in the constructor, same result
    }
    ngOnInit(): void {
        this.authorizationUpdates = this.authService.authorizationChangesObservable.subscribe((authorized) => {
            console.log(authorized); // <--- I can not see in the console
            this.router.navigate(['assessment'])
        })

     // doesn't work
     // this.authService.authorizationChanges.subscribe(
     //   (authorized) => { console.log('second try')})
    }

    ngOnDestroy(): void {
        this.authorizationUpdates.unsubscribe();
    }
}