2022-11-23

Importing RxJs in small Javascript project

I'm trying to import RxJs in a small javascript project using "import" in my script file but I keep on getting the following error:

Subscriber.js:10 Uncaught (in promise) TypeError: Cannot set properties of undefined (setting 'rxjs')

enter image description here

This is what my script file looks like:

<!DOCTYPE html>
<html lang="en-US">

  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="./styles/style.css">
    <title>Load RxJs using import</title>
  </head>

  <body>
    <h1>Importing RxJs Lib</h1>
    <div>
      <h1 id="info">'loading ...'</h1>
    </div>
    
    <!-- <script src="https://unpkg.com/rxjs@7.5.7/dist/bundles/rxjs.umd.js"></script> -->
    <script src="./js/script.js"></script>
  </body>
</html>

The script file looks like this:

// const updateUI = text => document.querySelectorAll('#info')[0].innerText = "finished";

const loadRxJsLib = async () => {
  await import('https://unpkg.com/rxjs@7.5.7/dist/bundles/rxjs.umd.js');
}

document.addEventListener('DOMContentLoaded', (event) => {
  loadRxJsLib();
});

document.addEventListener('readystatechange', (event) => {
  if (document.readyState === 'complete') {
    const { range } = rxjs;
    const { filter, map } = rxjs.operators;

    range(1, 200)
      .pipe(
        filter((x) => x % 2 === 1),
        map((x) => x + x)
      )
      .subscribe((x) => console.log(x))
  }
});

Any idea why it's not working? any comment will be highly appreciated



No comments:

Post a Comment