我目前正在开发一个使用Electron 类星体设置的小应用程序.
我的Electron 预加载.ts如下所示:
import { contextBridge} from 'electron'
import alasql from 'alasql'
contextBridge.exposeInMainWorld('Electron', {
importdata: () => importCsv()
})
export function importCsv () {
console.log('we get here')
return alasql.promise('SELECT * FROM CSV("/Users/niki/example_data.csv", {headers:true, seperators:","})')
.then(function (data) {
return data
})
在我的MainLayout.vue中,我这样称呼它:
const firstimport = window.electronApi.importdata()
然后,我继续使用Promise进行一些进一步的操作,所有这些操作都会收到未定义错误的Cannot Read属性.我在vscode中也得到了错误,即.ElectronApi不存在.从字面上讲,同样的事情也适用于我的Electron 应用程序,它不是用quasar-cli创建的,所以我不确定我做错了什么.
我的窗口也是这样创建的,如果它相关的话:
function createWindow () {
/**
* Initial window options
*/
mainWindow = new BrowserWindow({
icon: path.resolve(__dirname, 'icons/icon.png'), // tray icon
width: 1000,
height: 600,
useContentSize: true,
webPreferences: {
contextIsolation: true,
sandbox: false,
// More info: https://v2.quasar.dev/quasar-cli-vite/developing-electron-apps/electron-preload-script
preload: path.resolve(__dirname, process.env.QUASAR_ELECTRON_PRELOAD)
}
})
mainWindow.loadURL(process.env.APP_URL)
if (process.env.DEBUGGING) {
// if on DEV or Production with debug enabled
mainWindow.webContents.openDevTools()
} else {
// we're on production; no access to devtools pls
mainWindow.webContents.on('devtools-opened', () => {
mainWindow?.webContents.closeDevTools()
})
}
mainWindow.on('closed', () => {
mainWindow = undefined
})
}