electron vue 选择并获取目录路径

electron yekong 3983℃

nodejs仿写单页模板扒手时,遇到一个选择保存目录的功能,这里使用的是electron实现的ui,那么就用electron提供的api来实现吧。
保存目录的功能
electron vue页面选择并获取路径地址,需要vue触发事件,主进程监听触发选择,并返回给vue页面。

vue页面

import {ipcRenderer} from 'electron'
    getPath() {
      var that = this;
      ipcRenderer.send('open-directory-dialog', 'openDirectory');
      ipcRenderer.on('selectedItem', function (e, files) {
        that.loginForm.dir = files
      });
    },

主进程监听

import {ipcMain, dialog} from 'electron'
ipcMain.on('open-directory-dialog', function (event, p) {
            dialog.showOpenDialog({
                properties: [p],
                title: '请选择保存目录',
                buttonLabel: '选择'
            }).then(result => {
                console.log(result)
                event.sender.send('selectedItem', result.filePaths[0])
            })
        });
喜欢 (3)