我的项目代码中有一行是这样的,
const variableName = { ... }
export default variableName
variableName
一开始就写得正确吗?它不应该是
export const variableName = { ... } // import { variableName } from './file'
或者
export default variableName = { ... } // import variableName from './file'
将其导入另一个文件的正确方法是什么?
import { variableName } from 'file'
或者
import variableName from './file'
PS - 我看过 other answers , blogs也一样,但他们没有谈论导出 const 变量作为默认值。
最佳答案
这些都是正确的导出方式。
如果默认导出:
import myComponent from '...'
如果是命名导出 (export x;
)
import {x} from '...';
// OR
import {x as y} from '...';
console.log(y);
export const x =/* ... */;
只是
const x = /* ... */;
export x;
此简写不可用于默认导出,您不能编写:
export default const x = {};
关于javascript - 如何导入默认导出的 const,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53666656/