Tôi đang cố gắng sử dụng google đăng nhập với angular2 bằng cách làm theo câu hỏi này: Google Sign-In for Websites and Angular 2 using Typescriptđăng nhập bằng google bằng angular2 và bản in - nơi để lấy gapi?
Nhưng tôi nhận được một lỗi:
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)
Rõ ràng GAPI không được định nghĩa - mà tôi có thể hiểu được như tôi dường như chỉ tuyên bố một var trống.
mã hiện tại của tôi là như sau:
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();
});
}
}
Mẫu tải tốt, nó chỉ là chút gapi
.
Vì vậy, câu hỏi của tôi là: tôi đang thiếu gì? Làm thế nào để tôi cần xác định gapi
để nó hoạt động?
Đây là mã app.component chính của tôi:
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 { }
Xin chào, cảm ơn vì phản hồi! Bạn có thể chi tiết hơn một chút không? JS/TS với góc cạnh chắc chắn là một điểm yếu cho tôi. Tôi đã đi qua câu hỏi và thêm nó vào mẫu ứng dụng chính của tôi - không phải là giải pháp đẹp nhất. Nó vẫn không hoạt động và tôi không hoàn toàn chắc chắn nếu những gì tôi đã làm là đúng - hoặc làm thế nào để sửa nó. – Scironic
[Câu hỏi này] (https://stackoverflow.com/questions/19399419/angular-js-and-google-api-client-js-gapi) cung cấp một vài chiến lược để sử dụng API Google với Góc. Có vẻ như bạn đang gặp phải một điều kiện mà Angular cố gắng truy cập đối tượng 'gapi' trước khi kịch bản' platform.js' được tải. Thay vì thêm thẻ '
Thêm tệp sau vào cấu trúc dự án của bạn để xóa lỗi.
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/gapi/gapi.d.ts
Điều này sẽ làm GAPI sẵn trong dự án của bạn.
Nguồn
2016-10-21 14:27:14
Tôi cũng gặp vấn đề tương tự trong Angular v4.0. Cắt async hoãn từ các tập lệnh nền tảng API Google giải quyết vấn đề của tôi
Vấn đề của tôi là như dưới đây khi tôi sử dụng nền tảng api của Google như:
tôi giải quyết vấn đề của tôi bằng cách hủy bỏ async defer từ Nền tảng api của nền tảng Google như như dưới đây trong index.html của tôi:
Nguồn
2017-05-18 05:27:55 Amir
Loại bỏ _async defer_ trong NG 4 giải quyết vấn đề của tôi. Cảm ơn bạn! –
tôi giải quyết vấn đề này bằng cách gọi kịch bản sau đây trong chỉ mục.html
Lưu ý:
Sử dụng các kịch bản sau đây mà không async hoãn:
và sử dụng async hoãn trong kịch bản này
Nguồn
2017-06-13 10:33:39