gpt4 book ai didi

javascript - (<任何>窗口).SomeLibrary.SomeMethod(arg1,arg2)?

转载 作者:行者123 更新时间:2023-11-30 09:42:31 25 4
gpt4 key购买 nike

什么是 (<any>window)在 Angular2 中使用时?

我在研究 Stripe 支付库时发现的:

(<any>window).Stripe.card.createToken({
number: this.cardNumber,
exp_month: this.expiryMonth,
exp_year: this.expiryYear,
cvc: this.cvc
}, (status: number, response: any) => {
if (status === 200) {
this.message = `Success! Card token ${response.card.id}.`;
} else {
this.message = response.error.message;
}
});

http://blog.mgechev.com/2016/07/05/using-stripe-payment-with-angular-2/

看起来好像是让你在 Controller 内使用全局对象的东西,但我不太了解细节。似乎无法找到其他答案。

最佳答案

Sometimes you’ll end up in a situation where you’ll know more about a value than TypeScript does. Usually this will happen when you know the type of some entity could be more specific than its current type.

Type assertions are a way to tell the compiler “trust me, I know what I’m doing.”

它可能有两种形式:

(<any>window)

(window as any)

另见 https://www.typescriptlang.org/docs/handbook/basic-types.html#type-assertions

做同样事情的另一种方法是使用方括号语法

window['Stripe'].card...

在这种情况下,typescript 编译器也可以工作

关于javascript - (<任何>窗口).SomeLibrary.SomeMethod(arg1,arg2)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40494369/

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