导出在 ES6 中声明为常量的函数
Exporting a function declared as constant in ES6
我按照指南在我的 react-native
应用程序中实施 REDUX。我正在尝试实施 actions 但我的 eslint
一直在第 8 行出现此错误 -
[eslint] Prefer default export. (import/prefer-default-export)
我的代码是 -
import * as types from './types';
const incrementCounter = counterValue => ({
type: types.INCREMENT_COUNTER,
counterValue,
});
export { incrementCounter };
我的问题是在 ES6 中导出这个常量函数的正确方法是什么?
在config.js
// Declaration of const
const config = {
name: 'test'
};
export default config
在另一个文件中
// Using const
import * as config from '../config';
let name = config.name;
最简单的更改是将 as default
添加到您的 export { incrementCounter };
。但是,要将您的函数导出为默认导出,您宁愿编写
import * as types from './types';
export default counterValue => ({
type: types.INCREMENT_COUNTER,
counterValue,
});
或
import * as types from './types';
export default function incrementCounter(counterValue) {
return {
type: types.INCREMENT_COUNTER,
counterValue,
};
}
import/prefer-default-export
是一个有问题的规则,
使用 default exports
你将失去 类型一致性 而你的 IDE 不会'无法再帮助您进行重构、检查和代码完成。
您始终可以使用导入别名以不同的名称导入:import {incrementCounter as foo} from 'incrementCounter'
这可能是个人意见,但是,我强烈建议您保留 named exports
并编辑您的 .eslintrc
:
{
"rules": {
"import/prefer-default-export" : 0
}
}
我按照指南在我的 react-native
应用程序中实施 REDUX。我正在尝试实施 actions 但我的 eslint
一直在第 8 行出现此错误 -
[eslint] Prefer default export. (import/prefer-default-export)
我的代码是 -
import * as types from './types';
const incrementCounter = counterValue => ({
type: types.INCREMENT_COUNTER,
counterValue,
});
export { incrementCounter };
我的问题是在 ES6 中导出这个常量函数的正确方法是什么?
在config.js
// Declaration of const
const config = {
name: 'test'
};
export default config
在另一个文件中
// Using const
import * as config from '../config';
let name = config.name;
最简单的更改是将 as default
添加到您的 export { incrementCounter };
。但是,要将您的函数导出为默认导出,您宁愿编写
import * as types from './types';
export default counterValue => ({
type: types.INCREMENT_COUNTER,
counterValue,
});
或
import * as types from './types';
export default function incrementCounter(counterValue) {
return {
type: types.INCREMENT_COUNTER,
counterValue,
};
}
import/prefer-default-export
是一个有问题的规则,
使用 default exports
你将失去 类型一致性 而你的 IDE 不会'无法再帮助您进行重构、检查和代码完成。
您始终可以使用导入别名以不同的名称导入:import {incrementCounter as foo} from 'incrementCounter'
这可能是个人意见,但是,我强烈建议您保留 named exports
并编辑您的 .eslintrc
:
{
"rules": {
"import/prefer-default-export" : 0
}
}