2017-04-13 100 views
2

我想单击一个按钮在我的组件中打开文件夹对话框。这里是我想要做的事:如何从角度2组件打开电子对话框?

HTML:

<div> 
    <input class="u-full-width" placeholder="Folder" type="text" [(ngModel)]="folder"> 
    <button id="browse" class="button-primary" (click)="browse()">Browse</button> 
    <input id="fileInput" type="file" style="display: none" /> 
</div> 

component.ts

// var remote = require('remote'); 
// var dialog = remote.require('dialog'); 

    folder: string; 
    browse() { 
    dialog.showOpenDialog({title: 'Select a folder', properties: ['openDirectory']}, (folderPath) => { 
     if (folderPath === undefined){ 
      console.log("You didn't select a folder"); 
      return; 
     } 
     this.folder = folderPath; 
    }); 
    } 

所以,我该如何导入远程和对话?

回答

0

你很近。根据文档(https://github.com/electron/electron/blob/master/docs/api/dialog.md)在渲染器中使用它,您需要执行const {dialog} = require('electron').remote,因此您的第一个remote变量不是必需的。

+0

谢谢您的回答。但是,当我尝试这段代码时,我在浏览器控制台中出现错误:Uncaught TypeError:fs.​​existsSync不是Object中的函数。 (vendor.bundle.js:72631)...',并且该应用只显示“正在加载...” – newman

+0

我明白了。在您创建窗口的主要过程中,是否禁用节点集成?如果是这样,那么你可能需要将上面的代码放到预加载脚本中。更多细节见https://github.com/electron/electron/issues/5113 –

+0

实际上,我从[https://github.com/onehungrymind/electrogram](https://github.com/onehungrymind/electrogram)切换到另一个示例代码库github.com/onehungrymind/electrogram),它有打开对话框的代码。非常感谢你! – newman

0

您可以用ngx-electron库尝试

import {ElectronService} from 'ngx-electron' 

export class DialogHelper { 

    private static alert = new ElectronService().remote.dialog; 
}