【问题标题】:How can I resolve Argument of type 'null' is not assignable to parameter of type 'GoogleUser | undefined'如何解决“null”类型的参数不可分配给“GoogleUser |”类型的参数不明确的'
【发布时间】:2021-11-20 08:19:54
【问题描述】:

我正在尝试使用 gapi auth2 注册 google。以下代码位于 google-sigin.service.ts 中,我在其中定义了登录和注销功能。我在 this.subject.next(null) 中得到错误,错误是:

“null”类型的参数不能分配给“GoogleUser |”类型的参数未定义'。

export class GoogleSiginService {

  private auth2!: gapi.auth2.GoogleAuth 
  private subject = new ReplaySubject<gapi.auth2.GoogleUser>(1)

  constructor() { 
    gapi.load('auth2',()=> {
     this.auth2 = gapi.auth2.init(
       {
         client_id:'184******'
       }
     )
    })
   
  }
  public sigin(){
    this.auth2.signIn({
    scope:'https://www.googleapis.com/auth/gmail.readonly'
    }).then(user => {
    this.subject.next(user)
    }).catch( ()=> {
     this.subject.next(null)
    })
  }
  public signOut()
  { this.auth2.signOut().then(()=>
    {
      this.subject.next(null)
    })

  }
  public observable(): Observable<gapi.auth2.GoogleUser>
  {
    return this.subject.asObservable()
  }
}

【问题讨论】:

    标签: angular oauth-2.0 google-api-js-client


    【解决方案1】:

    要么将this.subject.next(null) 调用更改为this.subject.next(undefined),要么将subject = new ReplaySubject&lt;gapi.auth2.GoogleUser&gt;(1) 的类型签名扩展为gapi.auth2.GoogleUser | null

    【讨论】:

    • 成功了,非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 2022-06-24
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 2020-04-17
    • 1970-01-01
    相关资源
    最近更新 更多