我已经使用npm在我的Typescript项目中安装了navigo
和@types/navigo
。
我要使用
new Navigo(null, true, "#!");
如果不导入navigo
,则会收到错误消息
TS2686: 'Navigo' refers to a UMD global, but the current file is a module. Consider adding an import instead.
使用import Navigo from 'navigo';
时出现错误
TS1192: Module '"path/node_modules/@types/navigo/index"' has no default export.
使用import { Navigo } from 'navigo';
时我会得到
TS2497: Module '"path/node_modules/@types/navigo/index"' resolves to a non-module entity and cannot be imported using this construct.
使用import module = require("module");
时我会得到
TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.
这是怎么了?
使用此typedef https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/navigo/index.d.ts
答案 0 :(得分:0)
在我看来,这与您所安装的类型有关。深入了解Navigo的来源之后,它肯定会使用默认导出。使用https://www.npmjs.com/policies/unpublish的export =
语法导出类型文件。似乎这两个并不直接兼容:
export =
和import = require()
CommonJS和AMD通常都具有
exports
对象的概念,该对象包含模块的所有导出。它们还支持将
exports
对象替换为自定义的单个对象。默认导出旨在替代此行为;但是,两者是不兼容的。 TypeScript支持export =
为传统的CommonJS和AMD工作流程建模。
export =
语法指定从模块导出的单个对象。这可以是类,接口,名称空间,函数或枚举。使用
export =
导出模块时,必须使用TypeScript专用的import module = require("module")
导入模块。