为什么可以';我是否从javascript文件中按大括号导入属性?
我定义了一个javascript文件,如下所示:为什么可以';我是否从javascript文件中按大括号导入属性?,javascript,ecmascript-6,babeljs,Javascript,Ecmascript 6,Babeljs,我定义了一个javascript文件,如下所示: export default { parseOptions: { tolerant: true, raw: true, tokens: true, range: true, comment: true, }, syntaxType: { callback: 'callback', promise: 'promise', await: 'await', }, }; 我
export default {
parseOptions: {
tolerant: true,
raw: true,
tokens: true,
range: true,
comment: true,
},
syntaxType: {
callback: 'callback',
promise: 'promise',
await: 'await',
},
};
我使用以下代码将其导入其他文件:
import { syntaxType } from './options';
...
synaxType.callback
我得到一个错误,当我使用它时,
synaxType
没有定义。但是,如果我更改为以下代码,它将正常工作:
import options from './options';
options.synaxType.callback
我想知道我以前的进口有什么问题。我需要为此配置什么吗?以下是我的巴别塔配置:
{
"presets": [
["es2015"],
"stage-0",
"react",
"react-boilerplate"
],
"plugins": [
"transform-decorators-legacy",
"transform-class-properties",
"transform-async-to-generator",
"react-hot-loader/babel"
]
}
因为这就是
默认值的工作方式,所以实际上不需要为其指定名称。如果要使用大括号,则必须设置为不使用默认值,如:
export var options = {
parseOptions: {
tolerant: true,
raw: true,
tokens: true,
range: true,
comment: true,
},
syntaxType: {
callback: 'callback',
promise: 'promise',
await: 'await',
},
};
那你可以
import { options } from './options';
options.syntaxType
或
因为这就是默认值的工作方式,所以实际上不需要为其指定名称。如果要使用大括号,则必须设置为不使用默认值,如:
export var options = {
parseOptions: {
tolerant: true,
raw: true,
tokens: true,
range: true,
comment: true,
},
syntaxType: {
callback: 'callback',
promise: 'promise',
await: 'await',
},
};
那你可以
import { options } from './options';
options.syntaxType
或
语法
import options from './options';
将假定选项
是默认的导出对象。这就是options.synaxType.callback
起作用的原因
语法
import { syntaxType } from './options';
需要syntaxType
成为导出成员,例如:
export const syntaxType = {
callback: 'callback',
promise: 'promise',
await: 'await',
};
如果模块中存在上述构造,则该语法将起作用。语法
import options from './options';
将假定选项
是默认的导出对象。这就是options.synaxType.callback
起作用的原因
语法
import { syntaxType } from './options';
需要syntaxType
成为导出成员,例如:
export const syntaxType = {
callback: 'callback',
promise: 'promise',
await: 'await',
};
如果模块中存在上述构造,则该语法将起作用。有两种类型的导出,默认导出和命名导出。默认导出不需要名称,而命名导出需要名称
下面介绍如何导出默认值
export default "apple";
import iCanNameThisWhateverSinceItsADefaultExport from "./apple_module";
下面介绍如何导入默认值
export default "apple";
import iCanNameThisWhateverSinceItsADefaultExport from "./apple_module";
下面介绍如何导出命名值
export const apple = "apple";
export {apple} from "./apple_module";
下面介绍如何导入命名值
export const apple = "apple";
export {apple} from "./apple_module";
如果您想拥有对syntaxType
的命名访问权限,您有两个选项:
如果保持原样,只需将import
语句更改为:
import options from "./options";
const {syntaxType, parseOptions} = options;
// or access properties normally via (i.e.) options.syntaxType
或者您可以进行syntaxType
和parseOptions
命名导出:
export const parseOptions = {
tolerant: true,
raw: true,
tokens: true,
range: true,
comment: true,
};
export const syntaxType = {
callback: 'callback',
promise: 'promise',
await: 'await',
};
并使用
import {syntaxType, parseOptions} from "options";
这只是一个语法问题。一旦理解了语法,就应该能够很好地进行导入/导出。阅读优秀MDN文档中的所有内容和语句。有两种导出类型,默认导出和命名导出。默认导出不需要名称,而命名导出需要名称
下面介绍如何导出默认值
export default "apple";
import iCanNameThisWhateverSinceItsADefaultExport from "./apple_module";
下面介绍如何导入默认值
export default "apple";
import iCanNameThisWhateverSinceItsADefaultExport from "./apple_module";
下面介绍如何导出命名值
export const apple = "apple";
export {apple} from "./apple_module";
下面介绍如何导入命名值
export const apple = "apple";
export {apple} from "./apple_module";
如果您想拥有对syntaxType
的命名访问权限,您有两个选项:
如果保持原样,只需将import
语句更改为:
import options from "./options";
const {syntaxType, parseOptions} = options;
// or access properties normally via (i.e.) options.syntaxType
或者您可以进行syntaxType
和parseOptions
命名导出:
export const parseOptions = {
tolerant: true,
raw: true,
tokens: true,
range: true,
comment: true,
};
export const syntaxType = {
callback: 'callback',
promise: 'promise',
await: 'await',
};
并使用
import {syntaxType, parseOptions} from "options";
这只是一个语法问题。一旦理解了语法,就应该能够很好地进行导入/导出。阅读优秀MDN文档中的所有内容和陈述。synaxType
与syntaxType
不同synaxType
与syntaxType
不同default
您的代码是错误的。您没有声明var,并且导出了错误的对象。@loganfsmyth您是对的,我很快就写了它。我会编辑它。关于默认值,你是对的,但是你的代码是错的。您没有声明var,并且导出了错误的对象。@loganfsmyth您是对的,我很快就写了它。我会编辑它。完整的解释和更多的信息在:完整的解释和更多的信息在:不要忘记导入{apple as icanname this whateveriwantsceitsanaliasednamedexport}…
:-)不要忘记导入{apple as icanname this whateveriwantsceitsanaliasednamedexport}…
:-)