window.webkit 打字稿
window.webkit in Typescript
我需要将一段javascript代码重写成打字稿(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));
}
mpos
是 iOS 应用程序
ERROR in src/app/sms-validation/sms-validation.component.ts(98,17): error TS2339: Property 'webkit' does not exist on type 'Window'.
我认为 window.webkit
与标准相去甚远,因此它不是 Typescript 中 window
类型的一部分。您可以这样添加:
interface Window {
webkit?: any;
}
declare var window: Window;
注意 webkit?
表示 属性 是可选的,因此您应该检查 属性 是否存在。
您可以使用类型断言 (https://basarat.gitbooks.io/typescript/content/docs/types/type-assertion.html) 临时将 window
转换为 any
类型。不过,您将失去对该语句的智能感知。
(window as any).webkit.messageHandlers
我猜你正在使用 webView 如果你没有忘记 window.webkit 。
您必须先定义它,默认情况下没有对象(window):
window.webkit
只需添加即可:
let contentController = self.webView.configuration.userContentController
contentController.addScriptMessageHandler(self, name: "callbackHandler")
let config = WKWebViewConfiguration()
config.userContentController = contentController
let webView = WKWebView(frame: CGRect.zero, configuration: config)
// Add callback func
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
// message.name is "callbackHandler"
// message.body is ["programming":"js"]
}
现在您可以在浏览器中调用 env :
var message = {'programming':'js'};
window.webkit.messageHandlers.callbackHandler.postMessage(message);
参考:
我需要将一段javascript代码重写成打字稿(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));
}
mpos
是 iOS 应用程序
ERROR in src/app/sms-validation/sms-validation.component.ts(98,17): error TS2339: Property 'webkit' does not exist on type 'Window'.
我认为 window.webkit
与标准相去甚远,因此它不是 Typescript 中 window
类型的一部分。您可以这样添加:
interface Window {
webkit?: any;
}
declare var window: Window;
注意 webkit?
表示 属性 是可选的,因此您应该检查 属性 是否存在。
您可以使用类型断言 (https://basarat.gitbooks.io/typescript/content/docs/types/type-assertion.html) 临时将 window
转换为 any
类型。不过,您将失去对该语句的智能感知。
(window as any).webkit.messageHandlers
我猜你正在使用 webView 如果你没有忘记 window.webkit 。 您必须先定义它,默认情况下没有对象(window):
window.webkit
只需添加即可:
let contentController = self.webView.configuration.userContentController
contentController.addScriptMessageHandler(self, name: "callbackHandler")
let config = WKWebViewConfiguration()
config.userContentController = contentController
let webView = WKWebView(frame: CGRect.zero, configuration: config)
// Add callback func
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
// message.name is "callbackHandler"
// message.body is ["programming":"js"]
}
现在您可以在浏览器中调用 env :
var message = {'programming':'js'};
window.webkit.messageHandlers.callbackHandler.postMessage(message);
参考: