gpt4 book ai didi

javascript - 使用 angular2 和 typescript 进行谷歌登录 - 从哪里获得 gapi?

转载 作者:可可西里 更新时间:2023-11-01 02:05:24 33 4
gpt4 key购买 nike

我正在尝试通过以下问题使用 angular2 的 google 登录:Google Sign-In for Websites and Angular 2 using Typescript

但是我得到一个错误:

ORIGINAL EXCEPTION: ReferenceError: gapi is not defined
ORIGINAL STACKTRACE:
ReferenceError: gapi is not defined
at LoginAppComponent.ngAfterViewInit (http://localhost:3000/app/login.component.js:33:9)
at DebugAppView._View_AppComponent0.detectChangesInternal (AppComponent.template.js:46:68)
at DebugAppView.AppView.detectChanges (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:12143:18)
at DebugAppView.detectChanges (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:12247:48)
at DebugAppView.AppView.detectViewChildrenChanges (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:12169:23)
at DebugAppView.AppView.detectChangesInternal (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:12154:18)
at DebugAppView.AppView.detectChanges (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:12143:18)
at DebugAppView.detectChanges (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:12247:48)
at ViewRef_.detectChanges (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:10397:69)
at eval (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:9911:88)

显然 gapi 没有定义 - 我可以理解,因为我似乎只声明了一个空变量。

我目前的代码如下:

import {Component, NgZone} from "@angular/core";

declare var gapi: any;

@Component({
selector: "login",
templateUrl: "templates/login-template.html"
})
export class LoginAppComponent {
googleLoginButtonId = "google-login-button";
userAuthToken = null;
userDisplayName = "empty";

constructor(private _zone: NgZone) {
console.log(this);
}

// Angular hook that allows for interaction with elements inserted by the
// rendering of a view.
ngAfterViewInit() {
// Converts the Google login button stub to an actual button.
gapi.signin2.render(
this.googleLoginButtonId,
{
"onSuccess": this.onGoogleLoginSuccess,
"scope": "profile",
"theme": "dark"
});
}

// Triggered after a user successfully logs in using the Google external
// login provider.
onGoogleLoginSuccess = (loggedInUser) => {
this._zone.run(() => {
this.userAuthToken = loggedInUser.getAuthResponse().id_token;
this.userDisplayName = loggedInUser.getBasicProfile().getName();
});
}
}

模板加载正常,只是 gapi 位。

所以我的问题是:我错过了什么?我需要如何定义 gapi 才能工作?

这是我的主要 app.component 代码:

import { Component } from '@angular/core';
import { LoginAppComponent } from './login.component'

@Component({
selector: 'my-app',
template: `<script src="https://apis.google.com/js/platform.js?onload=onLoadCallback" async defer></script>
<script>
window.onLoadCallback = function(){
gapi.auth2.init({
client_id: 'filler_text_for_client_id.apps.googleusercontent.com'
});
}
</script>
<h1>Angular2 P.O.C.</h1>
<login></login>`,
directives: [LoginAppComponent]
})
export class AppComponent { }

最佳答案

您是否包含了 Google 平台 API 脚本?

<script src="https://apis.google.com/js/platform.js"></script>

参见 this question有关如何在执行代码之前等待 GAPI 脚本加载的说明。

关于javascript - 使用 angular2 和 typescript 进行谷歌登录 - 从哪里获得 gapi?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38664350/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com