打字稿中的 window.webkit

Nas*_*zah 3 javascript webkit typescript angular

我需要将 javascript 代码重写为 typescript (angular),网页由 IOS 应用程序或 Android 应用程序打开。我只想向应用程序发送消息。

如何向父应用程序发送消息或如何使用window.webkit

notifyTheApp(postData) {
  if (navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {
    if(window.webkit.messageHandlers)      
     window.webkit.messageHandlers.mpos.postMessage(JSON.stringify(postData));
    } 
    else {
      if(window.external.notify)
        window.external.notify(JSON.stringify(postData));
    }

Run Code Online (Sandbox Code Playgroud)

mpos 是iOS应用

src/app/sms-validation/sms-validation.component.ts(98,17) 中的错误:错误 TS2339:“窗口”类型上不存在属性“webkit”。

Fre*_*din 6

您可以使用类型断言(https://basarat.gitbooks.io/typescript/content/docs/types/type-assertion.html)暂时投windowany类型。但是,您将失去该声明的智能感知。

(window as any).webkit.messageHandlers
Run Code Online (Sandbox Code Playgroud)