[Angular] Angular Elements Intro

Make sure install the latest Angular v6 with Angular CLI. Checkout ght Github for the code.

1. Create a new application:

ng new elementApp

2. Install @angular/elements package:

npm i @angular/elements --savenpm i @webcomponents/custom-elements --save

3. Import polyfills in polyfills.ts:

/***************************************************************************************************
 * APPLICATION IMPORTS
 */

import ‘@webcomponents/custom-elements/src/native-shim‘;
import ‘@webcomponents/custom-elements/custom-elements.min‘;

4. Generate a component:

ng g c user-poll

5. Conver the element to angular elements:

import { BrowserModule } from ‘@angular/platform-browser‘;
import { NgModule, Injector } from ‘@angular/core‘;
import { createCustomElement } from ‘@angular/elements‘;
import { AppComponent } from ‘./app.component‘;
import { UserPollComponent } from ‘./user-poll/user-poll.component‘;

@NgModule({
  declarations: [ UserPollComponent],
  entryComponents: [UserPollComponent],
  imports: [BrowserModule]
})
export class AppModule {
  constructor(private injector: Injector) {}

  ngDoBootstrap() {
    const el = createCustomElement(UserPollComponent, { injector: this.injector });
    customElements.define(‘user-poll‘, el);
   }
}

6. Build process:

Install:

npm install fs-extra concat --save-dev
//elementApp/build-script.js

const fs = require(‘fs-extra‘);
const concat = require(‘concat‘);

(async function build() {

    const files =[
      ‘./dist/elementApp/polyfills.js‘,
      ‘./dist/elementApp/runtime.js‘,
      ‘./dist/elementApp/styles.css‘,
      ‘./dist/elementApp/main.js‘
    ]

    await fs.ensureDir(‘elements‘)

    await concat(files, ‘elements/user-poll.js‘)
    console.info(‘Elements created successfully!‘)

})()
//package.json

"build:elements": "ng build --prod --output-hashing none && node build-script.js"

Run:

npm run build:element

After concat all the files, we got:

Then we can use it in a test index.html:

<user-poll></user-poll>
<script src="elements/user-poll.js"></script>



Step 6. Now there is a new library can help to simplfy the process. checkout ngx-build-plus

原文地址:https://www.cnblogs.com/Answer1215/p/8992242.html

时间: 2024-10-18 09:59:16

[Angular] Angular Elements Intro的相关文章

[Angular 2] Directive intro and exportAs

First, What is directive, what is the difference between component and directive. For my understanding, component is something like 'canvas', 'form', 'table'... they have the template and their own functionality. It defines how a html tag should work

Angular Angular 部分2

临时笔记 太新了,现成的组件少, 自己封装一个 Angular UI 组件 Angular2采用自定义指令(Directive)方式加载jquery插件(别人跟着试了,无法实现) http://www.jb51.net/article/105121.htm 下面是几乎没有语法高亮的原文 http://www.cnblogs.com/liuyt/p/5810100.html AngularJS的组件封装 需求: 将一个Echarts封装成一个通用的AngularJS组件. 需求来源:Echarts

[Angular] Angular CLI

Create an app with routing config: ng new mynewapp --routing If you want to generate a new module with routing , you can also do: ng g m mynewModule --routing Define env variable: You can create a custom env file inside env folder. // environment.ch.

Angular - - angular.forEach、angular.extend

angular.forEach 调用迭代器函数取每一项目标的集合,它可以是一个对象或数组.迭代器函数与迭代器(value.key)一起调用,其中值是一个对象属性或数组元素的值,而数组元素是对象属性的关键或数组元素索引.为函数指定一个可选的上下文. 格式:angular.forEach(obj,iterator,[context]); obj:遍历的对象 iterator:迭代器 [content]: 对象为迭代器函数的上下文(this) 使用代码: var arr = [{ name: "A&q

Angular - - angular.equals

angular.equals 对比两个对象/值是否相等.支持值类型.正则表达式.数组和对象. 如果下列至少有一个是正确的,则将两个对象/值视为相等. 两个对象/值能通过===比较. 两个对象/值是同一类型/他们的属性一致并且通过angular.equals详细比较. 两者都是NaN. (在javascript中, NaN == NaN => false. 但是我们认为两个 NaN 是平等的) 两个值都代表相同的正则表达式 (在JavaScript里, /abc/ == /abc/ => fal

[Angular] Angular Global Keyboard Handling With EventManager

If we want to add global event handler, we can use 'EventManager' from '@angular/platform-broswer'. Now we have a modal component, we want to click 'Esc' key to close the modal. <au-modal class="auth-modal" *auModalOpenOnClick="[loginBut

Angular - - Angular数据类型判断

angular.isArray 判断括号内的值是否为数组. 格式:angular.isArray(value); value: 被判断是否为数组的值. --------------------------------------------------------------- angular.isDate 判断括号内的值是否是一个时间. 格式:angular.isDate(value); value:被判断是否为时间的值. -----------------------------------

Angular - - angular.Module

angular.Module Angular模块配置接口. 方法: provider(name,providerType); name:服务名称. providerType:创建一个服务的实例的构造函数. factory(name,providerFunction); name:服务名称. providerFunction:创建服务的实例的函数. service(name,constructor); name:服务名称. constructor:一个将被实例化的构造函数. value(name,

Angular - - angular.uppercase、angular.lowercase、angular.fromJson、angular.toJson

angular.uppercase 将指定的字符串转换成大写 格式:angular.uppercase(string); string:被转换成大写的字符串. 使用代码: var str = "ABCabc"; var upperCase = angular.uppercase(str);//ABCABC angular.lowercase 将指定的字符串转换成小写 格式:angular.lowercase(string); string:被转换成小写的字符串. 使用代码: var s