亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

未捕獲的 ReferenceError:需要未定義(電子)

未捕獲的 ReferenceError:需要未定義(電子)

慕斯王 2022-06-16 10:53:53
我正在嘗試在電子上創建一個報價小部件。對于渲染器進程,我創建了 index.js 并編碼如下console.log('from renderer');var request = require('request');const electron = require('electron');var url ="https://quotesondesign.com/wp-json/wp/v2/posts/?orderby=rand&_="+rnd;request(url, function(error, response, body) {    if(error)    {        document.getElementById("quote").innerHTML = 'Unable to fetch the quote plaese check the network connection';        return;    }    let bodyJSON = JSON.parse(body);    console.log(bodyJson);    let randomQuote = bodyJSON[0]["content"]["rendered"];    document.getElementById("quote").innerHTML = randomQuote;});index.html 有<div id="quote"></div><script src="index.js">    // require ('index.js');</script>如果我require ('index.js');在script標簽中使用,它不起作用。所以我用src="index.js". 現在渲染器進程工作但在控制臺上,它顯示"Uncaught ReferenceError: require is not defined at index.js:3" 我的第一個查詢是為什么標簽require ('index.js');上script不起作用index.html ,第二個查詢是如何解決Uncaught ReferenceError我index.js 的電子版本是 v8.2.0 和節點版本是 v12.16.1 和依賴項上package.json的問題如下:"dependencies": {        "request": "^2.88.2",        "require": "^2.4.20"    }請任何人幫助我。提前致謝。
查看完整描述

2 回答

?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

從 Electron 5 開始,渲染器進程中的節點集成默認是禁用的。為了解決這個問題,您需要nodeIntegration: true在實例化您的BrowserWindow.


// In the main process.

const { BrowserWindow } = require('electron')

const mainWindow = new BrowserWindow({

    width: 800,

    height: 600,

    webPreferences: {

      nodeIntegration: true

    }

  })

編輯:從 Electron 12 開始,您還需要定義contextIsolation: false才能執行此操作,因為標志的默認值已更改。


https://www.electronjs.org/docs/breaking-changes#default-changed-contextisolation-defaults-to-true


查看完整回答
反對 回復 2022-06-16
?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

require ('index.js');在 script 標簽中不起作用的原因是require沒有為瀏覽器定義。它僅針對 Node 定義。你得到ReferenceErrorin index.js 的原因是因為<script src="index.js>實際做的是在瀏覽器環境中運行 index.js 中的代碼。因此,由于它在瀏覽器中運行,因此require這里也沒有定義。



查看完整回答
反對 回復 2022-06-16
  • 2 回答
  • 0 關注
  • 188 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號