Alessandroinfo / angular-wrapped-in-web-component

An Angular App wrapped into a web-component

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Guide to wrap an Angular App with Routing in a Web Component

πŸ‘‹ Welcome the guide on how to include an entire Angular app with routing inside a web component!

Introduction

Will be used @angular/elements to create a web component in Angular, and the entire app will be included inside it.

Getting Started

  1. Clone the repository
  2. Install dependencies using npm install
  3. Build using ng build
  4. Package to compile the dist/*.*.js angular build into angular-in-web-component.js using npm run package
  5. Serve using npm run serve

Here are the relevant commands:

"start": "ng serve",
"build": "ng build",
"package": "cat ./dist/angular-in-web-component/{polyfills,runtime,main}.*.js > ./test-angular-web-component/angular-in-web-component.js && cp ./dist/angular-in-web-component/styles.*.css ./test-angular-web-component/styles.css",
"serve": "npx -p http-server http-server ./test-angular-web-component/ -o --cors --port 8080 -P http://localhost:8080?",

The package command will copy also the generated style file styles.css into the test-angular-web-component directory.

Here is the file tree:

β”œβ”€β”€ angular-in-web-component.js
β”œβ”€β”€ index.html
└── styles.css

Guide

Installation

Install @angular/elements with the following command:

npm i @angular/elements -D

AppModule

In app.module.ts, use @angular/elements to include the AppComponent component and define it with customElements.define.

Remember to set the same selector in AppComponent as the one used in customElements.define.

Here is the file tree of the app:

.
β”œβ”€β”€ app
β”‚   β”œβ”€β”€ app-routing.module.ts
β”‚   β”œβ”€β”€ app.component.html
β”‚   β”œβ”€β”€ app.component.scss
β”‚   β”œβ”€β”€ app.component.ts
β”‚   β”œβ”€β”€ app.module.ts
β”‚   └── home
β”‚       β”œβ”€β”€ container
β”‚       β”‚   β”œβ”€β”€ home.component.html
β”‚       β”‚   β”œβ”€β”€ home.component.scss
β”‚       β”‚   └── home.component.ts
β”‚       β”œβ”€β”€ home-routing.module.ts
β”‚       └── home.module.ts
β”œβ”€β”€ assets
β”œβ”€β”€ favicon.ico
β”œβ”€β”€ index.html
β”œβ”€β”€ main.ts
└── styles.scss

Routing

There is another module inside the app called home, which can be accessed at /home to test routing.

That's it! You can now include the Angular app with routing inside a web component.

Happy coding! πŸ’»

About

An Angular App wrapped into a web-component


Languages

Language:HTML 43.6%Language:SCSS 30.9%Language:TypeScript 25.5%