我可以从变量中导出默认值吗? [英] Can I export default from a variable?

查看:79
本文介绍了我可以从变量中导出默认值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据某些条件更改export { default } from 'MyFile',所以我可以用字符串变量替换文件吗?

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

目前我有这样的东西:

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?

还有一点要注意,我之所以这样做是因为我正在使用nextjs,并且我的其中一个页面在某些情况下必须是一个错误,否则它只是一个React组件,处理它的内容.因此,如果这不是解决问题的方法,该如何解决我的问题?

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?

推荐答案

像这样简单地导出:

export default exportFrom

或者,使用命名导出:

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

有关完整参考,请参见导入导出

For full reference see import and export

这篇关于我可以从变量中导出默认值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆