0

It seems this can possible in es5 and imagine NextI18Next got some functions in it:

module.exports = new NextI18Next({
  otherLanguages: ['ko'],
  defaultLanguage: 'en',
  ns: ['translations'],
  defaultNS: 'translations',
  localePath: path.resolve('../../public/locales'),
})

///////////
import { appWithTranslation, ... } from "..."
appWithTranslation()

So you can exports all class members simply just use module.exports, but in es6 >= version

You should use like this:

const i18n = new NextI18Next({
  otherLanguages: ['ko'],
  defaultLanguage: 'en',
  ns: ['translations'],
  defaultNS: 'translations',
  localePath: path.resolve('../../public/locales'),
})

export default i18n
export const appWithTranslation = i18n.appWithTranslation
export const useTranslation = i18n.useTranslation
// ...

///////// 
import { appWithTranslation, ... } from "..."
appWithTranslation()

It seems you can't exports all class members in es5 does.

Is there are ways to exports all class members simply like es5?

JillAndMe
  • 3,989
  • 4
  • 30
  • 57

2 Answers2

0

Static Imports:

For static imports, you can use Destructuring assignment do something like:

export default new NextI18Next({
  otherLanguages: ['ko'],
  defaultLanguage: 'en',
  ns: ['translations'],
  defaultNS: 'translations',
  localePath: path.resolve('../../public/locales'),
})

and then import like so:

import myModule from "...";
const { appWithTranslation, ... } = myModule;

I believe that for static imports, all exports need to be named or default; you can't import anything that isn't explicitly exported. In this case appWithTranslation isn't explicitly exported, so I think the best you can do is with two lines, importing the default object with one line, and destructuring assignment with another.

See also https://stackoverflow.com/a/43987935

Dynamic imports

In the case of dynamic imports, you can use destructuring assignment to import it in one line:

(async function () {
  const { default: { appWithTranslation } } = await import('./filename.js');
  appWithTranslation();
})();
Steve
  • 10,435
  • 15
  • 21
0

No, you have to spell out named exports explicitly in ES6.

In most cases, you shouldn't export class member - especially unbound methods - individually anyway if they are already part of an exported instance object.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375