Javascript ReferenceError:在src中添加Rx.js时未定义Observable

时间:2018-09-15 12:41:39

标签: javascript

我正在尝试使observables可以在我的html上工作,但我一直收到此错误:

ReferenceError:未定义可观察到的

我正在使用Firefox version: 62.0

代码如下:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <script type="text/javascript"  src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.4.0/Rx.js"></script>
    <script>
    	const numberObservable = new Observable((observer) => {
        	observer.next(5);
        	observer.next(10);
    	});
    
    	numberObservable.subscribe(value => console.log(value));
    </script>
    <title></title> 
</head>
<body>
</body>
</html>

如何使它正常工作?

2 个答案:

答案 0 :(得分:1)

您只需要使用Rx.Observable而不是简单的Observable

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <script type="text/javascript"  src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.4.0/Rx.js"></script>
    <script>
    	const numberObservable = new Rx.Observable((observer) => {
        	observer.next(5);
        	observer.next(10);
    	});
    
    	numberObservable.subscribe(value => console.log(value));
    </script>
    <title></title> 
</head>
<body>
</body>
</html>

答案 1 :(得分:0)

应该{​​{1}}注意名称空间。