如何导出在 ES6 中保留名称的变量?
How to export a variable which has reserved name in ES6?
我们知道,这是ES6的导出变量的语法。
export const LANGUAGE = 'JavaScript'
这是在 ES5 中声明相同代码的一种方式:
exports.LANGUAGE = 'JavaScript'
但在某些其他情况下它不起作用,例如保留字和包含空格的名称:
exports.true = '#true'
exports['some text'] = 'text'
那么在 ES6 中声明导出的正确方法是什么?
您不能使用带有保留字的 export const varName = 'Value'
语法;请阅读 ECMAScript 6 modules: the final syntax 上的以下声明:
Note that you can’t use reserved words (such as default and new) as variable names, but you can use them as names for exports (you can also use them as property names in ECMAScript 5). If you want to directly import such named exports, you have to rename them to proper variables names.
据此,您似乎应该能够按照以下方式做一些事情:
const true_ = '#true';
export { true_ as true };
请注意,这也会导致导入端出现问题。您很可能需要 re-alias 导入。例如,
import { true as true_ } from '...';
我们知道,这是ES6的导出变量的语法。
export const LANGUAGE = 'JavaScript'
这是在 ES5 中声明相同代码的一种方式:
exports.LANGUAGE = 'JavaScript'
但在某些其他情况下它不起作用,例如保留字和包含空格的名称:
exports.true = '#true'
exports['some text'] = 'text'
那么在 ES6 中声明导出的正确方法是什么?
您不能使用带有保留字的 export const varName = 'Value'
语法;请阅读 ECMAScript 6 modules: the final syntax 上的以下声明:
Note that you can’t use reserved words (such as default and new) as variable names, but you can use them as names for exports (you can also use them as property names in ECMAScript 5). If you want to directly import such named exports, you have to rename them to proper variables names.
据此,您似乎应该能够按照以下方式做一些事情:
const true_ = '#true';
export { true_ as true };
请注意,这也会导致导入端出现问题。您很可能需要 re-alias 导入。例如,
import { true as true_ } from '...';