admin管理员组

文章数量:1022956

How do I export as default multiple imported modules?

I could do something like the code below, but if I were to import multiple files the boilerplate could grow quickly. Is there a shorthand way to achieve this?

import Foo from './Foo'
import Bar from './Bar'
//...

export default {
    Foo,
    Bar
    //...
}

Note that in my code above, I'm not exporting multiple values. I'm importing multiple values, accumulating them, and exporting them as a single object. I intend to refer to the reexported values by Baz.Foo or Baz.Bar, assuming the code above is Baz.js.

How do I export as default multiple imported modules?

I could do something like the code below, but if I were to import multiple files the boilerplate could grow quickly. Is there a shorthand way to achieve this?

import Foo from './Foo'
import Bar from './Bar'
//...

export default {
    Foo,
    Bar
    //...
}

Note that in my code above, I'm not exporting multiple values. I'm importing multiple values, accumulating them, and exporting them as a single object. I intend to refer to the reexported values by Baz.Foo or Baz.Bar, assuming the code above is Baz.js.

Share Improve this question edited Jul 29, 2019 at 2:18 tys asked Jul 25, 2019 at 8:08 tystys 6996 silver badges20 bronze badges 2
  • Read this page: stackoverflow./questions/38340500/… – R.tbr Commented Jul 25, 2019 at 8:22
  • Finally found a shorthand way that works with default exports. I'm using it in my application now :) – dandeto Commented Nov 5, 2019 at 6:24
Add a ment  | 

4 Answers 4

Reset to default 2

Instead of accumulating boilerplate from importing lots of files to simply re-export them, you can do this:

// In library.js
export { default as Foo } from "./Foo.js";
export { default as Bar } from "./Bar.js";

// In new_file.js
import { Foo, Bar } from "../lib/library.js"
var phooey = new Foo();

This selects whatever you are exporting as default and gives it an alias.

export default is not meant to be used with multiple exports: https://developer.mozilla/en-US/docs/web/javascript/reference/statements/export#Using_the_default_export

If we want to export a single value or to have a fallback value for your module, you could use a default export

You should use named exports like this:

export {
    Foo,
    Bar
    //...
}

In es6 there are two types of exports named and default exports.

Since you want to export default assign all the modules to an object like below

Const modules = {Foo,  Bar }
export default modules;

Then in other files use like below

Import modules from 'path/yourfiles'
modules.Foo.xxx
modules.Bar.xxx
import * as name from "module-name";

This will be a great fit for your application. And when you want to access the object, you can use

name.object = ...;

And let me know, if it is right.

How do I export as default multiple imported modules?

I could do something like the code below, but if I were to import multiple files the boilerplate could grow quickly. Is there a shorthand way to achieve this?

import Foo from './Foo'
import Bar from './Bar'
//...

export default {
    Foo,
    Bar
    //...
}

Note that in my code above, I'm not exporting multiple values. I'm importing multiple values, accumulating them, and exporting them as a single object. I intend to refer to the reexported values by Baz.Foo or Baz.Bar, assuming the code above is Baz.js.

How do I export as default multiple imported modules?

I could do something like the code below, but if I were to import multiple files the boilerplate could grow quickly. Is there a shorthand way to achieve this?

import Foo from './Foo'
import Bar from './Bar'
//...

export default {
    Foo,
    Bar
    //...
}

Note that in my code above, I'm not exporting multiple values. I'm importing multiple values, accumulating them, and exporting them as a single object. I intend to refer to the reexported values by Baz.Foo or Baz.Bar, assuming the code above is Baz.js.

Share Improve this question edited Jul 29, 2019 at 2:18 tys asked Jul 25, 2019 at 8:08 tystys 6996 silver badges20 bronze badges 2
  • Read this page: stackoverflow./questions/38340500/… – R.tbr Commented Jul 25, 2019 at 8:22
  • Finally found a shorthand way that works with default exports. I'm using it in my application now :) – dandeto Commented Nov 5, 2019 at 6:24
Add a ment  | 

4 Answers 4

Reset to default 2

Instead of accumulating boilerplate from importing lots of files to simply re-export them, you can do this:

// In library.js
export { default as Foo } from "./Foo.js";
export { default as Bar } from "./Bar.js";

// In new_file.js
import { Foo, Bar } from "../lib/library.js"
var phooey = new Foo();

This selects whatever you are exporting as default and gives it an alias.

export default is not meant to be used with multiple exports: https://developer.mozilla/en-US/docs/web/javascript/reference/statements/export#Using_the_default_export

If we want to export a single value or to have a fallback value for your module, you could use a default export

You should use named exports like this:

export {
    Foo,
    Bar
    //...
}

In es6 there are two types of exports named and default exports.

Since you want to export default assign all the modules to an object like below

Const modules = {Foo,  Bar }
export default modules;

Then in other files use like below

Import modules from 'path/yourfiles'
modules.Foo.xxx
modules.Bar.xxx
import * as name from "module-name";

This will be a great fit for your application. And when you want to access the object, you can use

name.object = ...;

And let me know, if it is right.

本文标签: javascriptExport as default multiple imported modulesStack Overflow