Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to create a minimalist pure js app with rxjs

Trying to create a minimalist pure js app with rxjs.

Flow:

  1. npm install rxjs
  2. index.html
  3. myscript.js

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?

like image 208
Goliath2019 Avatar asked Sep 10 '25 14:09

Goliath2019


1 Answers

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>
like image 107
kos Avatar answered Sep 12 '25 03:09

kos