GithubHelp home page GithubHelp logo

alessandroinfo / angular-elements-example Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 224 KB

Example of creating a web component using Angular Elements

License: MIT License

TypeScript 63.80% HTML 25.31% SCSS 10.89%

angular-elements-example's Introduction

๐Ÿ› ๏ธ Web Components with Angular Elements

This is a repository that showcases how to create web-components in Angular with @angular/elements. ๐Ÿš€

Angular Elements: What are they? ๐Ÿค”

Angular Elements are Angular components packaged as custom elements (also known as Web Components), a web standard for defining new HTML elements in a framework-agnostic way. They allow you to encapsulate your logic and styles in reusable components that can be used across different applications, regardless of the underlying technology stack. ๐ŸŒ

Project Structure ๐Ÿ“‚

The repository consists of the following files and directories:

โ”œโ”€โ”€ test-web-components
โ”‚   โ”œโ”€โ”€ index.html
โ”‚   โ””โ”€โ”€ tabs-web-components.js
โ”œโ”€โ”€ app
โ”‚   โ”œโ”€โ”€ app.module.ts
โ”‚   โ”œโ”€โ”€ tabs-new-way
โ”‚   โ”‚   โ”œโ”€โ”€ tabs-new-way.component.html
โ”‚   โ”‚   โ”œโ”€โ”€ tabs-new-way.component.scss
โ”‚   โ”‚   โ””โ”€โ”€ tabs-new-way.component.ts
โ”‚   โ””โ”€โ”€ tabs-old-way
โ”‚       โ”œโ”€โ”€ tabs-old-way.component.html
โ”‚       โ”œโ”€โ”€ tabs-old-way.component.scss
โ”‚       โ”œโ”€โ”€ tabs-old-way.component.ts
โ”‚       โ””โ”€โ”€ tabs-old-way.module.ts
โ””โ”€โ”€ main.ts

Project Components

The repository contains two methods to create web components:

  • tabs-old-way: The older method in which a component was included in a module.
  • tabs-new-way: The newer method that allows for standalone components.

Directory test-web-components

  • Inside test-web-components will be builded the tabs-web-components.js that contain the concat of angular builds.
  • There you find also index.html that contain the tags of web components:
...
<h1>Tabs old way to implement web-component</h1>
<tabs-old></tabs-old>

<h1>Tabs new way to implement web-component</h1>
<tabs-new></tabs-new>
...

main.ts

This file contains the bootstrap logic for the Angular application.

In this project, the file also includes a script that defines and registers the tabs-new custom element.

app.module.ts

This file defines the main module of the Angular application.

In this project, the file also includes a script that defines and registers the tabs-old custom element.

Getting Started ๐Ÿš€

  • Clone this repository:

    • git clone https://github.com/your-username/your-project.git
  • Install the dependencies:

    • npm install
  • Build the project:

    • npm run build
  • Concat the js files generated by Angular build:

    • npm run package
  • Serve the application:

    • npm run test-components

Result ๐ŸŒ

Differences ๐Ÿฉป

The two different methods showcased in the repository demonstrate two different approaches for creating web components in Angular using @angular/elements.

The first method, which is referred to as the "old way", involves creating a module for the web component and then registering it with customElements.define() in the ngDoBootstrap() method of the AppModule.

The second method, which is referred to as the "new way", involves creating a standalone component and then registering it with customElements.define() outside of Angular's bootstrapping process.

Both methods ultimately result in the creation of a custom web component, but the second method allows for more flexibility in terms of when and how the component is loaded, and also allows for the component to be used outside of an Angular context.

angular-elements-example's People

Contributors

alessandroinfo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.