Cannot read property of undefined (reading 'showOpenDialog')
P粉826429907
P粉826429907 2024-03-26 11:45:51
0
1
451

I keep getting this error:

TypeError: Cannot read properties of undefined (reading 'showOpenDialog')

How to call showOpenDialog() with minimal changes?

I'm using the new Quasar/Electron with Webpack 5.

This is the openFile() method in my vue file:

    openFile () {
      window.electronApi.dialog.showOpenDialog({
        title: 'Load File',
        properties: ['openFile']
      }).then(result => {
        const filename = result.filePaths[0]

        if (filename) {
          const extension = getExtensionName(filename)
          console.log(extension)
        } else {
          console.log('No file selected')
        }
      }).catch(err => {
        console.log(FILE + 'Error: ' + err)
      })
    },

This is what is declared in electro-preload.js:

import { contextBridge} from 'electron'

contextBridge.exposeInMainWorld('electronApi', {
  dialog: require('electron').dialog,
  showOpenDialog: require('electron').showOpenDialog
})

P粉826429907
P粉826429907

reply all(1)
P粉788765679

Thanks to @creative learner for the idea! This works!

import { contextBridge} from 'electron'
import { dialog } from '@electron/remote' 

contextBridge.exposeInMainWorld('electronApi', {
  dialog: dialog
})
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template