我正在尝试使用typescript生成webview的实例 .
我正在使用electronic-forge的react-typescript模板 .
我尝试了以下方法:
import {Component} from 'react';
import * as React from 'react';
import {WebviewTag} from 'electron';
class MediaWebView extends Component<{ url: string }, {}> {
renderWebVierw() {
const myWebView: WebviewTag /* error here */ =
(<webview src={this.props.url}
autosize='on'
nodeintegration='on'
disablewebsecurity='on'
webpreferences='allowRunningInsecureContent'
style={webViewStyle}
/>);
return myWebView;
}
这将呈现webview,但会引发以下错误: TS2322: Type 'Element' is not assignable to type 'WebviewTag'. Property 'addEventListener' is missing in type 'Element'
.
我无法承认事件或将代码注入webview .
我不知道应该分配什么类型,但我似乎无法找到它 .
实现webview的正确方法是什么,并且可以访问其方法 using typescript ?
1 回答
有同样的问题 . 问题是因为没有
as
,编译器不会返回WebViewTag对象,并且等式的左后方并不关心,因为WebViewTag继承自HtmlElement . 在这种情况下,as SomeClass
会将对象强制转换为您尝试将其强制转换为的任何类型,如果类型匹配,则返回该对象,如果不匹配,则返回null .