为什么我不能在打字稿中从 `export default {}` `import {}`

Why can't I `import {}` from `export default {}` in typescript

我有一个 typescript 文件:

#util.ts
export default {name: 'name'}

我从另一个文件导入它:

import {name} from './util'

编译失败,错误为util has no exported member name。我应该如何在 typescript 中导入它?

您的 util.ts 文件导出 默认导出 ,而您请求 命名导出 。您必须更改导入以请求默认导出:

import name from './util';

看区别in this article

如果您想访问 name 字段的值,请考虑在您的 util.ts:

中使用 命名导出
export const name = 'name';

或:

const name = 'name';

export {
  name,
}

然后导入:

import { name } from '/util';