electron-vue?運行報錯?Object.fromEntries?is?not?a?function的解決方案
1. 背景
最近研究一款桌面端應用的開發(fā)框架electron-vue,在按照 electron-vue官方文檔 操作之后操作如下,Object.fromEntries is not a function。

2. 解決方案
2.1 第一步:安裝依賴
在項目目錄安裝 polyfill-object.fromentries,執(zhí)行以下命令:
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 新增的一個靜態(tài)方法,用于將鍵值對列表(如數(shù)組)轉換為對象。如果在當前環(huán)境中不支持該方法,可以使用 polyfill 來提供類似功能。
具體來說,Object.fromEntries() 方法接收一個二維數(shù)組作為參數(shù),第一維表示鍵名,第二維表示對應的鍵值,然后返回由這些鍵值對組成的對象。例如:
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 實現(xiàn)類似的功能:
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 函數(shù)檢查當前環(huán)境是否支持 Object.fromEntries() 方法,如果不支持,則定義一個同名的函數(shù)并實現(xiàn)對應的功能。這里使用了 for…of 循環(huán)以及解構賦值語法來遍歷鍵值對列表,并將其轉換為對象。
到此這篇關于electron-vue 運行報錯 Object.fromEntries is not a function的文章就介紹到這了,更多相關electron-vue 運行報錯內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue中this.$router和this.$route的區(qū)別及push()方法
這篇文章主要給大家介紹了關于Vue中this.$router和this.$route的區(qū)別及push()方法的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-05-05
vue+axios 攔截器實現(xiàn)統(tǒng)一token的案例
這篇文章主要介紹了vue+axios 攔截器實現(xiàn)統(tǒng)一token的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
vue-cli3.0+element-ui上傳組件el-upload的使用
這篇文章主要介紹了vue-cli3.0+element-ui上傳組件el-upload的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
vue.js的computed,filter,get,set的用法及區(qū)別詳解
下面小編就為大家分享一篇vue.js的computed,filter,get,set的用法及區(qū)別詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03

