我目前正在开发一个没有任何模块加载器的打字稿项目。
现在我正在尝试在我的项目中集成 rxjs。但我每次都会出错ReferenceError: exports is not defined
。是否有必要使用模块加载器,如果是,你能帮我设置吗?我目前正在编译 ts 文件tsc -w
const mock = of('Hello');
mock.subscribe(res => console.log(res));
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HM</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="assets/hm-logo.png">
<link rel="stylesheet" type="text/css" href="../node_modules/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<script src="./main.js"></script>
</body>
</html>
感谢@toskv,我设法在没有任何模块加载器的情况下将 rxjs 集成到打字稿中。为此,我需要考虑以下步骤:
将 cdn 链接添加到根HTML(在 head 标记内),如下所示:
<script src="https://unpkg.com/[email protected]/bundles/rxjs.umd.min.js"></script>
添加一个带有全局变量的 rxjs.d.ts 文件:
declare var rxjs;
在 tsconfig.json 文件中包含 rxjs.d.ts 文件,例如:
"include": [
"src/**/*",
"rxjs.d.ts"
]
现在您可以在 ts 文件中使用 rxjs,例如:
const { Observable, Subject, ReplaySubject, from, of, range } = rxjs;
const { map, filter, switchMap } = rxjs.operators;
range(1, 200)
.pipe(filter(x => x % 2 === 1), map(x => x + x))
.subscribe(x => console.log(x));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句