electron-vue 运行报错 Object.fromEntries is not a function的解决方案
1. 背景
最近研究一款桌面端应用的开发框架electron-vue,在按照 electron-vue官方文档 操作之后操作如下,Object.fromEntries is not a function。

2. 解决方案
2.1 第一步:安装依赖
在项目目录安装 polyfill-object.fromentries,执行以下命令:
npm i polyfill-object.fromentries
2.2 第二步:项目中引入
在/.electron-vue/dev-client.js文件中引入上述安装的插件:
完成代码如下
const hotClient = require('webpack-hot-middleware/client?noInfo=true&reload=true')
// 引入插件
import 'polyfill-object.fromentries';
hotClient.subscribe(event => {
/**
* Reload browser when HTMLWebpackPlugin emits a new index.html
*
* Currently disabled until jantimon/html-webpack-plugin#680 is resolved.
* https://github.com/SimulatedGREG/electron-vue/issues/437
* https://github.com/jantimon/html-webpack-plugin/issues/680
*/
// if (event.action === 'reload') {
// window.location.reload()
// }
/**
* Notify `mainWindow` when `main` process is compiling,
* giving notice for an expected reload of the `electron` process
*/
if (event.action === 'compiling') {
document.body.innerHTML += `
<style>
#dev-client {
background: #4fc08d;
border-radius: 4px;
bottom: 20px;
box-shadow: 0 4px 5px 0 rgba(0, 0, 0, 0.14), 0 1px 10px 0 rgba(0, 0, 0, 0.12), 0 2px 4px -1px rgba(0, 0, 0, 0.3);
color: #fff;
font-family: 'Source Sans Pro', sans-serif;
left: 20px;
padding: 8px 12px;
position: absolute;
}
</style>
<div id="dev-client">
Compiling Main Process...
</div>
`
}
})3. 组件详解
Object.fromEntries() 是 ECMAScript 2019 新增的一个静态方法,用于将键值对列表(如数组)转换为对象。如果在当前环境中不支持该方法,可以使用 polyfill 来提供类似功能。
具体来说,Object.fromEntries() 方法接收一个二维数组作为参数,第一维表示键名,第二维表示对应的键值,然后返回由这些键值对组成的对象。例如:
const arr = [
['name', 'Alice'],
['age', 18],
['gender', 'female']
];
const obj = Object.fromEntries(arr);
console.log(obj); // { name: 'Alice', age: 18, gender: 'female' }当 Object.fromEntries() 方法不可用时,可以通过以下 polyfill 实现类似的功能:
if (!Object.fromEntries) {
Object.fromEntries = function(entries) {
if (!entries || !entries[Symbol.iterator]) {
throw new Error('Object.fromEntries() requires an iterable argument');
}
const obj = {};
for (let [key, value] of entries) {
obj[key] = value;
}
return obj;
};
}这个 polyfill 函数检查当前环境是否支持 Object.fromEntries() 方法,如果不支持,则定义一个同名的函数并实现对应的功能。这里使用了 for…of 循环以及解构赋值语法来遍历键值对列表,并将其转换为对象。
到此这篇关于electron-vue 运行报错 Object.fromEntries is not a function的文章就介绍到这了,更多相关electron-vue 运行报错内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Vue中this.$router和this.$route的区别及push()方法
这篇文章主要给大家介绍了关于Vue中this.$router和this.$route的区别及push()方法的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2021-05-05
vue-cli3.0+element-ui上传组件el-upload的使用
这篇文章主要介绍了vue-cli3.0+element-ui上传组件el-upload的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-12-12
vue.js的computed,filter,get,set的用法及区别详解
下面小编就为大家分享一篇vue.js的computed,filter,get,set的用法及区别详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2018-03-03


最新评论