Electron 报错 Uncaught (in promise) TypeError: Cannot read property ‘showMessageBox‘ of undefined

Uncaught (in promise) TypeError: Cannot read property ‘showMessageBox’ of undefined

Electron 项目叙述:

Vue3 + TypeScript + electron 使用 electron-builder 集成 electron-vue 自带最高可以选择electron^13.0.0 (嫌弃。。。)

electron

毕竟目前最新版本都到electron^23.2.0+了,但是没得办法Vue3香的嘞,原不想写折腾原生HTML+CSS+JS那一套,也不想写 Angular React ~

暂时找不到更好的集成方案,再加上项目进度迫在眉睫实属无奈之举,当然如果你有更好的方案可以推荐一下最好是Vue3 Vue vue…,在这里先向各位说一声谢谢啦…

我想要实现的用法 在渲染进程中即Vue Setup中使用 remote 模块

原先写法:

在 background.ts 中
// 引入
import * as remote from '@electron/remote/main';
// 初始化
remote.initialize();
// 监听应用的启动事件
app.on('ready', async () => {
// 创建设置子窗口
const window = new BrowserWindow({
	// 配置此次省略...
	{
      // 使用remote模块,electron10x还是多少来着 版本之后废除了,需要自己安装...
      enableRemoteModule: true, // 老版本14x之前开启就可以使用了
	}
});
// 使用remote模块,electron13之后 开启remote
// remote.enable(window.webContents);
})
在 渲染进程 vue 中
<script lang="ts" setup>
import {ipcRenderer} from 'electron';
import {dialog} from '@electron/remote'
// 打开dialog
const openDialog = () => {
      dialog.showMessageBox({
        // 其它省略...
        message: '香的嘞~'
      })
})
<script/>
在 调试控制台 中…

在这里插入图片描述

Uncaught (in promise) TypeError: Cannot read property 'showMessageBox' of undefined

这才是重点解决方法 在渲染进程 Vue 中:

<script lang="ts" setup>
import {ipcRenderer} from 'electron';
// import {dialog} from '@electron/remote' // 原来引入方式electron13版本之后的使用方式
const { remote } = window.require('electron') // + New 引入方式(没办法版本太老了这也就是为什么我嫌弃的原因,因为这种引入方式在 electron13x 版本之后废除了......)
// 打开dialog
const openDialog = () => {
	  // 原
      /* 
      dialog.showMessageBox({
        // 其它省略...
        message: '香的嘞~'
      })
      */
		// 改
	   remote.dialog.showMessageBox({
        // 其它省略...
        message: '不香的嘞~'
      })
})
<script/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答:根据提供的引用内容来看,出现了一个错误提示信息"Uncaught (in promise) TypeError: Failed to fetch"。这个错误通常是由于浏览器的安全机制导致的。这个错误可能和跨域资源共享(CORS)策略有关。CORS是一种机制,用于控制浏览器是否允许跨域请求。在默认情况下,浏览器是不允许跨域请求的。如果请求的地址不在同一个域名下,浏览器会发送一个预检请求(preflight request)来检查服务器是否允许跨域请求。如果服务器返回的响应不符合CORS策略,浏览器就会阻止请求并抛出相应的错误。引用中提到了一个CORS错误的示例,说明了预检请求不通过的情况。 要解决这个问题,你可以采取以下几种方法: 1.在服务器端进行设置,允许跨域请求。可以通过设置响应头Access-Control-Allow-Origin来允许特定域名的跨域请求。具体的设置方式可以参考服务器端框架的文档或者相关的CORS教程。 2.使用代理服务器。可以将请求发送到同一个域名下的代理服务器上,然后在代理服务器上再发送请求到目标地址。这样就避免了跨域请求。 3.使用JSONP。JSONP是一种跨域请求的方式,可以通过动态添加<script>标签来获取跨域的数据。 请根据你的具体情况选择合适的方法来解决这个问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [springboot报错Uncaught (in promise) TypeError: Failed to fetch](https://blog.csdn.net/Houyanxiu/article/details/131583019)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [解决使用fetch发送json格式的post请求报错问题](https://blog.csdn.net/Y_soybean_milk/article/details/117306517)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [解决:Uncaught (in promise) TypeError: Failed to fetch 的问题](https://blog.csdn.net/weixin_43405300/article/details/127973854)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值