Trying to create a minimalist pure js app with rxjs.
Flow:
Index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="node_modules/rxjs/bundles/rxjs.umd.min.js" ></script>
<script src="myscript.js"></script>
<title>Document</title>
</head>
<body>
</body>
</html>
myscript.js:
rxjs.Observable.FromEvent(document, 'mousemove')
.subscribe(()=> console.log("text!"));
I get the error "Uncaught TypeError: rxjs.Observable.FromEvent is not a function"
attempt to import failed:
import 'rxjs/add/observable/fromEvent'; //SyntaxError: Unexpected string
or
import { fromEvent } from 'rxjs'; //SyntaxError: Unexpected token {
What am I doing wrong? How to try rxjs on pure js?
For RxJS v6 take factory functions from rxjs
and operators from rxjs.operators
.
Here's an example with a CDN source:
var { fromEvent } = rxjs;
var { map } = rxjs.operators;
var theButton = document.getElementById('the-btn');
fromEvent(theButton, 'click').pipe(
// pipe some operators here
map(() => Date.now())
).subscribe(date => {
console.log('Clicked at ' + date);
});
<script src="https://unpkg.com/[email protected]/bundles/rxjs.umd.min.js"></script>
<button
id="the-btn"
>Clicker</button>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With