亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

electron-vue?運(yùn)行報(bào)錯(cuò)?Object.fromEntries?is?not?a?function的解決方案

 更新時(shí)間:2023年05月30日 09:21:36   作者:林凡塵coding  
Object.fromEntries()?是?ECMAScript?2019?新增的一個(gè)靜態(tài)方法,用于將鍵值對(duì)列表(如數(shù)組)轉(zhuǎn)換為對(duì)象,如果在當(dāng)前環(huán)境中不支持該方法,可以使用?polyfill?來(lái)提供類似功能,接下來(lái)通過(guò)本文介紹electron-vue?運(yùn)行報(bào)錯(cuò)?Object.fromEntries?is?not?a?function的解決方案

1. 背景

最近研究一款桌面端應(yīng)用的開(kāi)發(fā)框架electron-vue,在按照 electron-vue官方文檔 操作之后操作如下,Object.fromEntries is not a function。

2. 解決方案

2.1 第一步:安裝依賴

在項(xiàng)目目錄安裝 polyfill-object.fromentries,執(zhí)行以下命令:

npm i polyfill-object.fromentries

2.2 第二步:項(xiàng)目中引入

/.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 新增的一個(gè)靜態(tài)方法,用于將鍵值對(duì)列表(如數(shù)組)轉(zhuǎn)換為對(duì)象。如果在當(dāng)前環(huán)境中不支持該方法,可以使用 polyfill 來(lái)提供類似功能。

具體來(lái)說(shuō),Object.fromEntries() 方法接收一個(gè)二維數(shù)組作為參數(shù),第一維表示鍵名,第二維表示對(duì)應(yīng)的鍵值,然后返回由這些鍵值對(duì)組成的對(duì)象。例如:

const arr = [
  ['name', 'Alice'],
  ['age', 18],
  ['gender', 'female']
];
const obj = Object.fromEntries(arr);
console.log(obj); // { name: 'Alice', age: 18, gender: 'female' }

當(dāng) Object.fromEntries() 方法不可用時(shí),可以通過(guò)以下 polyfill 實(shí)現(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;
  };
}

這個(gè) polyfill 函數(shù)檢查當(dāng)前環(huán)境是否支持 Object.fromEntries() 方法,如果不支持,則定義一個(gè)同名的函數(shù)并實(shí)現(xiàn)對(duì)應(yīng)的功能。這里使用了 for…of 循環(huán)以及解構(gòu)賦值語(yǔ)法來(lái)遍歷鍵值對(duì)列表,并將其轉(zhuǎn)換為對(duì)象。

到此這篇關(guān)于electron-vue 運(yùn)行報(bào)錯(cuò) Object.fromEntries is not a function的文章就介紹到這了,更多相關(guān)electron-vue 運(yùn)行報(bào)錯(cuò)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論