What is the standard way to create an Angular Component in the browser?

140 Views Asked by At

I am trying to create a simple JS/ESM based Angular example. It has been a while since I have been in the angular space and I see there are really 2 options

  1. Using the UMD lib (I would like to avoid this)
  2. Use the ESM2015 folder and load using ESM (this is what I would like to do)

I try doing this like...

<html>
    <head></head>
    <body ng-app="jrg-module">
        <jrg-element></jrg-element>
        <jrg-app></jrg-app>
        <script type="module">
            import { Component } from "//unpkg.com/@angular/core/esm2015/index.js";
            import { platformBrowserDynamic } from "//unpkg.com/@angular/platform-browser-dynamic/esm2015/index.js"
            import {ShadowElement, CREATE_ELEMENT} from "//unpkg.com/@jrg/ui/target/jrg-ui.esm.mjs";
            class JrgElement extends ShadowElement {
                constructor() {
                    super("<h1>CustomElement</h1>");
                    this.render();
                }
            }
            CREATE_ELEMENT("jrg-element", JrgElement, {});
            const MyComponent = Component({
                selector:"jrg-app",
                template:"<h1>Angular</h1>"
            }).Class({
                constructor: function() {}
            });
            const app = platformBrowserDynamic().bootstrapModule(MyComponent)
        </script>
    </body>
</html>

But (after taking forever to download 500+ files) I get

Uncaught TypeError: Error resolving module specifier “rxjs”. Relative module specifiers must start with “./”, “../” or “/”.

Can I use the ESM version in the browser or do I have to use UMD? If I can use ESM from the browser is there a link to an example?

I swear Angular used to have a dropdown for their examples where you could switch between TS and JS but I don't see it now.

1

There are 1 best solutions below

8
On

Have you tried out the website stackblitz it has plenty of angular examples of setting up a new project.

You can also do ng new my-app from the cli to generate a new project locally.

Angular has moved away from javascript in favor of typescript, so you will have to use ESM