本文介绍了使用打字稿控制Electron中的WebView对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用打字稿生成的实例。 / p>

我正在使用电子伪造的react-typescript模板。



我尝试了以下操作:

 从'react'导入{Component}; 
进口*作为来自反应的React;
从 electron导入{WebviewTag};

类MediaWebView扩展了Component< {url:string},{}> {

renderWebVierw(){
const myWebView:WebviewTag / *此处错误* / =
(< webview src = {this.props.url}
自动调整大小='on'
nodeintegration ='on'
disablewebsecurity ='on'
webpreferences ='allowRunningInsecureContent'
style = {webViewStyle}
/>));
返回myWebView;
}

这将呈现Web视图,但会引发以下错误: TS2322:类型'Element'不可分配给'WebviewTag'类型。属性'addEventListener'在'元素'类型中丢失。



我无法订阅事件或将代码注入Web视图。



我不知道应该分配什么类型,但是我似乎找不到它。



什么是实现Webview并能够使用打字稿访问其方法的正确方法?

解决方案
  const webview:WebViewTag =(< webview />)作为WebViewTag; 

有相同的问题。问题是因为没有 as ,编译器不知道您正在做的事情是返回WebViewTag对象,而等式的左后边没有呢?不必担心,因为WebViewTag继承自HtmlElement。在这种情况下, as SomeClass 会将对象强制转换为您尝试将其强制转换为的任何类型,如果类型匹配,则如果对象不匹配,它将返回对象,它只会返回null。


I'm trying to generate a instance of a webview using typescript.

I'm using electron-forge's react-typescript template.

I tried the following:

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;
  }

This renders the webview but throws the following error: TS2322: Type 'Element' is not assignable to type 'WebviewTag'. Property 'addEventListener' is missing in type 'Element'.

I can't suscribe to events or inject code into the webview.

I don't know what type should I be assigning, but I can't seem to find it.

What's the correct way to implement a webview, and have access to its methods using typescript?

解决方案
const webview: WebViewTag = (<webview />) as WebViewTag;

Had the same problem. The issue is because without the as, the compiler doesn't know that what you're doing is returning a WebViewTag object, and the left-hind side of the equation doesn't care because WebViewTag inherits from HtmlElement. In this case as SomeClass will coerce the object into whatever type you try to cast it to, and if the types match, then it will return the object, if they don't, it will just return null.

这篇关于使用打字稿控制Electron中的WebView对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 11:18