【发布时间】:2018-05-22 22:36:36
【问题描述】:
没有实现 Angular 4/5/6 的 Square 结帐表单,所以我试图通过创建一个测试组件来弄清楚这可能需要什么,但我不知道如何获得参考将他们的全局对象 SqPaymentForm 导入 Angular 并让 TypeScript 不会抛出错误,指出它在窗口对象上不存在 window.SqPaymentForm,因为它确实存在。
谁能告诉我如何让 TypeScript 识别 window.SqPaymentForm 的存在?
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
@NgModule({
imports: [
CommonModule
],
declarations: [],
providers: [
{ provide: 'SQUARE', useFactory: getSquare }
]
})
export class SquareModule { }
export function getSquare() {
return (typeof window !== undefined)
// TypeScript indicates it doesn't exist on Window
? window.SqPaymentForm
: null;
}
【问题讨论】:
-
这似乎是一个时间问题,你能推迟调用支付表单直到它被初始化吗?
-
它应该被加载。该脚本在 index.html 的 中加载,但现在尝试将引用加载为提供程序。我希望我可以将它用作提供程序,并且当结帐组件加载时,我可以调用类似于他们的示例的东西,但由于
window.SqPaymentForm根据 TypeScript 不存在而无法构建它来测试该理论跨度>
标签: typescript square