[英]Is there any difference between Electron built-in module and the one installed with npm? How to access to the electron object from other modules?
如果您查看此電子安裝手冊 ,可以閱讀應該以這種方式安裝電子:
npm install electron --save-dev
所以我做到了。 但是,如果您查看其他文檔,他們會說:
當使用Electron的內置模塊時,您可能會遇到如下錯誤:
> require('electron').webFrame.setZoomFactor(1.0) Uncaught TypeError: Cannot read property 'setZoomLevel' of undefined
這是因為您在本地或全局安裝了npm electronic模塊,該模塊覆蓋了Electron的內置模塊。
我不知道“本地”的意思是這樣的(沒有--save-dev
):
npm install electron
要檢查安裝是否正確:
要驗證您是否使用了正確的內置模塊,可以打印電子模塊的路徑:
console.log(require.resolve('electron'))
然后檢查它是否具有以下格式:
"/path/to/Electron.app/Contents/Resources/atom.asar/renderer/api/lib/exports/electron.js"
如果它是類似node_modules / electron / index.js的文件,那么您必須刪除npm電子模塊,或者重命名它。
在我的應用程序中的結果是
...\app_folder\node_modules\electron\dist\resources\electron.asar\browser\api\exports\electron.js
我可以從main.js
文件訪問電子對象。 這工作正常:
const {app} = require('electron');
但是,如果我在其他js文件中執行此操作(我需要main.js中的這些文件),則會得到未定義的值。 這正常嗎? 我是否需要將電子對象作為參數發送給其他模塊?
他們也這么說,但我考慮到了:
但是,如果您使用內置模塊但仍然出現此錯誤,則很可能您在錯誤的過程中使用了該模塊。 例如,electron.app只能在主進程中使用,而electron.webFrame僅在渲染器進程中可用。
該文檔是否仍是最新的? 我應該如何安裝Electron來使內置模塊正常工作?
實際上,如果我在其他模塊中這樣做
const electron = require('electron');
console.log(electron)
console.log(electron.app)
將打印對象:
{ clipboard: [Getter],
crashReporter: [Getter],
nativeImage: [Getter],
shell: [Getter],
app: [Getter],
autoUpdater: [Getter],
BrowserView: [Getter],
BrowserWindow: [Getter],
contentTracing: [Getter],
dialog: [Getter],
globalShortcut: [Getter],
ipcMain: [Getter],
inAppPurchase: [Getter],
Menu: [Getter],
MenuItem: [Getter],
net: [Getter],
netLog: [Getter],
Notification: [Getter],
powerMonitor: [Getter],
powerSaveBlocker: [Getter],
protocol: [Getter],
screen: [Getter],
session: [Getter],
systemPreferences: [Getter],
TopLevelWindow: [Getter],
TouchBar: [Getter],
Tray: [Getter],
View: [Getter],
webContents: [Getter],
WebContentsView: [Getter] }
App {
_events:
{ login: [Function],
'certificate-error': [Function],
'select-client-certificate': [Function],
quit: [Function],
'web-contents-created': [Function],
'session-created': [Function],
'will-quit': [Function],
ready: [ [Function], [Function] ],
'window-all-closed': [Function] },
_eventsCount: 9,
_maxListeners: undefined,
whenReady: [Function: whenReady],
setApplicationMenu: [Function: setApplicationMenu],
getApplicationMenu: [Function: getApplicationMenu],
commandLine:
{ appendSwitch: [Function: appendSwitch],
appendArgument: [Function: appendArgument] },
getAppMetrics: [Function],
isPackaged: false,
allowNTLMCredentialsForAllDomains: [Function],
releaseSingleInstance: [Function],
makeSingleInstance: [Function] }
但是,如果我嘗試獲取用戶數據路徑
const __user_data = electron.app.getPath('userData');
我收到此錯誤:
Cannot read property 'getPath' of undefined
我想知道為什么會因為應用程序存在而發生這種情況,但是如果我運行app.getPath()
應用程序不再存在。 electron.remote
也發生了類似的事情,我也嘗試過taht,即使這是主要過程。
除了對安裝路徑的疑問外,我還解決了該問題。 我在應用程序的不同位置都需要此文件。 有時我從主進程調用它,在其他情況下從渲染器進程調用它。 因此,我必須這樣做以支持兩種情況:
var app = null;
if (typeof(electron.remote) !== 'undefined') {
app = electron.remote.app;
} else {
app = electron.app
}
const __user_data = app.getPath('userData');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.