Can I export default from a variable?

2019-08-25 02:31发布

问题:

I would like to change my export { default } from 'MyFile' depending on certain conditions, so can I replace the file with a string variable?

Currently I have something like this:

let exportFrom
if(SOME_CONDITION) {
  exportFrom = '../Something/Blah'
} else {
   exportFrom = './SomethingElse'
}
export { default } from exportFrom

This currently doesn't work as I get:

Parsing error: Unexpected token

Is there a way to do this?

It is also important to note, that the reason I am doing this in the first place is because I am using nextjs, and one of my pages needs to be an error in certain conditions, otherwise it is just a react component which handles the content of it. So if this is not the way to do it, how do I solve my issue?

回答1:

Simply export like this:

export default exportFrom

Or, using named export:

export {
  exportFrom,
  // you_can_export_any_number,
  // of_variables
}

For full reference see import and export



回答2:

Just drop {}

export default exportFrom

Here's MDN ref