Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A step-by-step guide and sample project for Angular Elements / Custom Elements

NotificationsYou must be signed in to change notification settings

plaetzchen79/elements-test

Repository files navigation

This is a little project to show basics of Angular Elements.It is a step by step guide.

What is Angular Elements

Problem

Angular Modules and Components are great when using in Angular Environments and the Angular ecosystem.

Have you ever dreamed of using Angular components in just every HTML-Page and other environments likeREACT?

That is what custom-elements are designed for.The custom-elements standard ist supported by most modern browsers.

From angular-io:A custom-element behaves like any other HTML element, and does not require any special knowledge of Angular terms or usage conventions.Sounds great.

Even if you stay in the Angular ecosystem, thecreation of dynamic components gets much more easier withAngular Elements. (Am i the only one not liking the ComponentFactory ? ;-))

Summary Benefits

  • Angular independent
  • Easy dynamic component creation

Solution

Angular Elements is the key to transform Angular Components into standarized web-components.

This means that the Angular Framework (and everything else needed) is bundled into one package.

And with Angular we can transport every component into a custom-element with this function:createCustomElement().

HowTo

Add needed stuff (polyfill) to you app.Using this:ng add @angular/elements --name=elements-test.

  1. In tsconfig.json change target to"target": "es2015",

  2. Create a common Angular component

  3. Convert your new Angular component to a custom-element withcreateCustomElement() (in yourapp.ts)

  4. Register to the browsers CustomElement-Registry with js-functioncustomElements.define

  5. Add component to entry components inapp.moduleentryComponents: [SendMessageComponent],Add schema the folowing schema:schemas: [ CUSTOM_ELEMENTS_SCHEMA ]

  6. Use the custom-element inapp.html

Transformation

InputSince we have no upper/lower case distinction in html inputs will be transformedto dash-separated lowercase.Example:textInput --> < text-input=''>

EventsEvents will be tranformed to CustomEvents. Here we have upper/lower case.

HTML-Example:

<send-message-elementtext-input="Custom element input text"(send)="dataSend($event)"></send-message-element>

Dynamic creation

  1. To create a custom element just usedocument.createElement
  2. To get properties and typings addNgElement & WithProperties<MyComponent>
  3. For Events add an event-listener withaddEventListener
  4. Add new element to the DOM wirhappendChild

Example:

constsendMessageElement=document.createElement('send-message-element')asNgElement&WithProperties<SendMessageComponent>;sendMessageElement.textInput='Input for new one';sendMessageElement.addEventListener('send',($event)=>this.logSend($event));document.body.appendChild(sendMessageElement);

Export for other languages

To use our custom element in other technologies (e.g. REACT) we have to createone single js file.

  1. In our componentent change css encapsulation toencapsulation: ViewEncapsulation.Native
  2. create an ng prod build withng build --prod --output-hashing false
  3. bundle all output *.bundle.js from thedist folder files to a single js-file
  4. Write a script and an npm command in package.json for steps 1 and 2

Running unit tests

Actually i do not care for testing here (shame on me).Runng test to execute the unit tests viaKarma.

Further help

Quick introduction video

About

A step-by-step guide and sample project for Angular Elements / Custom Elements

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp