AWS Amplify signedIn 变量未定义

AWS Amplify signedIn Variable is undefined

我正在使用这个 site 中的 aws-amplify 我注意到这个网站上,this.signedIn 没有在这个 link 的任何地方使用。我正在尝试使用 is 作为指示器 true/false 值来判断用户是否已登录。

这是代码:

  constructor(public mediaObserver: MediaObserver, private amplifyService: AmplifyService ){
this.amplifyService.authStateChange$
.subscribe(authState => {
    this.signedIn = authState.state === 'signedIn';
  });
  console.log('This is from home constructor for signedIn boolean value ', this.signedIn)

this.signedIn is sent to the console window as : undefined

AppComponent 没有为 signedIn 设置初始值,所以默认情况下它将是 undefined:

export class AppComponent {
    signedIn: boolean;
    ...
}

您的 console.log(...) 在组件构造函数中,因此当日志调用被命中时 - 值仍然是 undefined

您仍然可以在模板中引用 signedIn,因为 undefined 值将解析为 false。或者您可以更明确地使用 signedIn: boolean = false 进行初始化。