Can I export default from a variable?

帅比萌擦擦* 提交于 2019-12-11 16:42: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



来源:https://stackoverflow.com/questions/55226386/can-i-export-default-from-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!