Equivalent of requiring a subproperty in ES6 import

后端 未结 2 856
旧时难觅i
旧时难觅i 2021-01-22 06:24

I have an existing require:

const {dialog} = require(\'electron\').remote;

I started using Babel for ES6, and would like to

2条回答
  •  攒了一身酷
    2021-01-22 06:54

    ECMAScript module syntax doesn't allow deep destructuring. In fact it doesn't destructure at all. Import statements create live bindings between modules.

    Here is great blog post written by Ben Nadel. It should shed some light on bindings: http://www.bennadel.com/blog/3131-the-import-statement-creates-a-live-view-of-modules-in-es6-and-typescript-in-angular-2.htm

    So by doing

    import electron from 'electron';
    const {dialog} = electron.remote;
    

    electron is such binding. By doing destructuring assignment dialog is normal constant and it won't be "bound" to electron module (it won't update).

提交回复
热议问题