如何从角度2组件打开电子对话框?

new*_*man 4 electron angular

我想单击一个按钮以在组件中打开文件夹对话框。这是我正在尝试做的事情:

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>
Run Code Online (Sandbox Code Playgroud)

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;
    });
  }
Run Code Online (Sandbox Code Playgroud)

那么,如何导入遥控器和对话框?

小智 5

只需使用es6 import导入“ remote”模块,然后您的ts文件就会像

import { remote } from 'electron';

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


Sea*_* W. 0

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