将行为主体集成到Typescript应用程序中
我试图将rxjs集成到我的typescript项目中,但最终出现以下错误将行为主体集成到Typescript应用程序中,typescript,rxjs,Typescript,Rxjs,我试图将rxjs集成到我的typescript项目中,但最终出现以下错误 BehaviorSubject.js:5 Uncaught ReferenceError: exports is not defined at BehaviorSubject.js:5 testSubject.ts:22 Uncaught TypeError: Cannot read property 'BehaviorSubject' of undefined 包含行为主体的文件 import { Behav
BehaviorSubject.js:5 Uncaught ReferenceError: exports is not defined
at BehaviorSubject.js:5
testSubject.ts:22 Uncaught TypeError: Cannot read property 'BehaviorSubject' of undefined
包含行为主体的文件
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
export class TestProject {
constructor() {
let testString = "Eric";
let currentNameSubject$ = new BehaviorSubject(testString);
currentNameSubject$.subscribe(val => {
alert(val);
});
currentNameSubject$.next('Obama');
currentNameSubject$.next('Jacob');
}
}
tsconfig.json
"compilerOptions": {
/* Basic Options */
"target": "es5",
"module": "amd",
"paths": {
"rxjs/*": [ "./node_modules/rxjs/*" ],
"rxjs-compact/*": [ "./node_modules/rxjs-compact/*" ]
},
"inlineSources": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"lib": [ "es6", "dom" ],
"esModuleInterop": true
}
package.json:
rxjs: 6.6.7,
rxjs-compat: 6.6.7,
typescript: 4.2.4
我将模块改为“commonjs”,这对我来说很有用。“路径”包含别名,这意味着您可以将属性简化为包名。“path”的每个值都是相对于“baseURL”的路径列表。这就是为什么您应该将其添加到tsconfig.json中
这里是我正在使用的测试项目:
tsconfig.json
test.ts
磨合节点
嘿,我试过你的,但还是出现了错误,
App.js:21 Uncaught ReferenceError:exports没有定义
你使用什么编程语言或为什么平台开发?只有前端的typescript和后端的.net我认为你不能currentNameSubject$。订阅
,您需要currentNameSubject$.asObservable().subscribe
谢谢,但这是另一部分,即使我删除了该行,我也无法运行。我甚至只尝试了构造函数中的前两行
{
"compilerOptions": {
/* Basic Options */
"target": "es5",
"module": "commonjs",
"baseUrl": ".",
"paths": {
"rxjs": [ "node_modules/rxjs/*" ],
"rxjs-compact": [ "node_modules/rxjs-compact/*" ]
},
"inlineSources": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"lib": [ "es6", "dom" ],
"esModuleInterop": true
},
"exclude": ["node_modules"]
}
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
export class TestProject {
constructor() {
console.log(`it works`);
let testString = 'Eric';
let currentNameSubject$ = new BehaviorSubject(testString);
currentNameSubject$.subscribe(val => {
console.log(val);
});
currentNameSubject$.next('Obama');
currentNameSubject$.next('Jacob');
}
}
const test = new TestProject();
tsc --sourceMap && node test.js