【发布时间】:2019-01-02 12:17:53
【问题描述】:
只能偶尔访问 Google 选择器。每次打开应用程序时,Google Picker Popup 都不会打开。
我正在 Angular 6 中实现 Google Picker API。 我在 angular 的 assets 文件夹中为连接 Google API 背后的逻辑保留了单独的文件,并在 document.createElement("script") 的帮助下附加了 javascript 文件。 我在 app.component.html 中有一个用于 getElementById 的 Anchor 标记。
app.component.html
<a routerLink="/" id="AllFilePick" #AllFilePick> Button </a>
app.component.ts
import { Component, ViewChild, ElementRef, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
@ViewChild('AllFilePick') AllFilePick: ElementRef;
constructor(private elementRef: ElementRef) { }
ngOnInit() {
var s1 = document.createElement("script");
s1.type = "text/javascript";
s1.src = "../assets/api-script.js";
this.elementRef.nativeElement.appendChild(s1);
var s2 = document.createElement("script");
s2.src = "https://www.google.com/jsapi?key=<API_KEY>";
this.elementRef.nativeElement.appendChild(s2);
var s3 = document.createElement("script");
s3.src = "https://apis.google.com/js/client.js?onload=SetPicker";
this.elementRef.nativeElement.appendChild(s3);
// console.log(this.AllFilePick.nativeElement);
console.log(s1);
console.log(s2);
console.log(s3);
}
}
我什至尝试使用 ngAfterViewInit,构造函数来附加脚本标签。
assets/api-script.js
function SetPicker() {
var picker = new FilePicker(
{
apiKey: ‘<API_KEY>’,
clientId: ‘<CLIENT_ID>’,
buttonEl: document.getElementById("AllFilePick"),
onClick: function (file) {
PopupCenter('https://drive.google.com/file/d/' + file.id + '/view', "", 1026, 500);
}
}
);
}
function PopupCenter(url, title, w, h)
{
//.....
}
function FilePicker(User)
{
//Configuration
//....
}
以上完整版代码运行正常,但弹出窗口很少打开,偶尔会打开。 只有在多次刷新应用程序或第二天打开应用程序后才会弹出触发器。 Picker 在 Angular 中无法正常工作。
【问题讨论】:
标签: javascript angular typescript google-api google-picker