没有实现 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;
}
想通了,我必须创建自己的类型,但无法弄清楚如何通过.d.ts
加载它,所以,现在,它在 Square 模块中声明:
declare global {
interface Window {
SqPaymentForm: any;
}
}
或将window.SqPaymentForm
替换为:
(<any>window).SqPaymentForm