[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:
ng add @angular/elements --project-name=<your_project_name>
3. Generate a component:
ng g c course-title
4. Conver the element to angular elements: First we need to add our component to 'entryComponents'
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: [CourseTitleComponent],
imports: [BrowserModule]
})
export class AppModule {
constructor(private injector: Injector) {} }
5. Connect Custom Element Web API inside our component:
// course title constructor(
private injector: Injector
)} ngOnInit() {
const htmlElement = createCustomElement(CourseTitleComponent, {injector: this.injector});
customElements.define('couse-title', htmlElement )
}
6. Now the Angular Element should already work in the broswer. If we want to use Angular Element inside Angular Application, we should add 'schemas':
@NgModule({
imports: [
CommonModule
],
...
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
7. If you want to dynamic insert Angular Element into Angular application, such as:
export class AppComponent {
addEl() {
const container = document.getElementById('container');
container.innerHTML = '<course-title></course-title>';
}
}
You need to add polyfill in order to make it work:
npm i --save-dev @webcomponents/webcomponentsjs
Then add into polyfills.js:
..
* APPLICATION IMPORTS
*/ import '@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js';
[Angular] Angular Elements Intro的更多相关文章
- [Angular 2] Directive intro and exportAs
First, What is directive, what is the difference between component and directive. For my understandi ...
- [Angular] Angular CDK Intro
1. Installl latest @angular/cli: sudo npm i -g @angular/cli@next The version I used is:6.0.0-rc.10 2 ...
- [Angular 2] Select From Multiple Nested Angular 2 Elements
You have complete control over the elements you nest inside of your component instance by using sele ...
- [Angular 2] Simple intro Http
To use http, we need to import the HTTP_PROVIDER, so that we can inject http to other component: imp ...
- Angular - - angular.Module
angular.Module Angular模块配置接口. 方法: provider(name,providerType); name:服务名称. providerType:创建一个服务的实例的构造函 ...
- Angular - - angular.injector、angular.module
angular.injector 创建一个injector对象, 调用injector对象的方法可用于获取服务以及依赖注入. 格式:angular.injector(modules); modules ...
- Angular - - Angular数据类型判断
angular.isArray 判断括号内的值是否为数组. 格式:angular.isArray(value); value: 被判断是否为数组的值. ------------------------ ...
- Angular - - angular.uppercase、angular.lowercase、angular.fromJson、angular.toJson
angular.uppercase 将指定的字符串转换成大写 格式:angular.uppercase(string); string:被转换成大写的字符串. 使用代码: var str = &quo ...
- Angular - - angular.bind、angular.bootstrap、angular.copy
angular.bind 返回一个调用self的函数fn(self代表fn里的this).可以给fn提供参数args(*).这个功能也被称为局部操作,以区别功能. 格式:angular.bind(se ...
随机推荐
- JavaWeb 之 AJAX
Ajax ajax:AJAX 是与服务器交换数据的艺术,它在不重载全部页面的情况下,实现了对部分网页的更新 AJAX:Asynchronous JavaScript and XML,异步 javasc ...
- javascript函数笔记
函数是一个具有特定功能的语句块.函数的定义使用关键字 function,语法如下: function funcName ([parameters]){ statements; [return表达式;] ...
- [SPOJ SEQN] [hdu3439]Sequence
题目就是求C(n,k)*H(n - k)%m 0<= k<= n <=10^9, 1 <= m <= 10^5, n != 0 其中H(n)是错排第n项. 对于C(n,k ...
- [YC703]ゴミ拾い Easy
[YC703]ゴミ拾い Easy 题目大意: 二维平面内有\(n(n\le3\times10^5)\)个人和\(n\)个物品,第\(i\)个人在\((a_i,0)\)上,第\(i\)个物品在\((x_ ...
- Acdream 1738 世风日下的哗啦啦族I 树套树
世风日下的哗啦啦族I Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acdream.info/problem?pid=1738 Descri ...
- UIImagePickerController导航字体颜色和背景
创建UIImagePickerController // 创建图片选择器 UIImagePickerController *picker = [[UIImagePickerController all ...
- Effective JavaScript Item 51 在类数组对象上重用数组方法
Array.prototype对象上的标准方法被设计为也能够在其他对象上重用 - 即使不是继承自Array的对象. 因此,在JavaScript中存折一些类数组对象(Array-like Object ...
- Homebrew-macOS缺失的软件包管理器(简称brew)
[简介] brew又叫Homebrew,是Mac OSX上的软件包管理工具,能在Mac中方便的安装软件或者卸载软件,只需要一个简单的命令,非常方便 [遇到的问题] 在真正了解软件包管理工具之前,一直是 ...
- VS 2017 取消结构参考线的显示
Visual studio 中的结构参考线如下所示 其可以通过如下方式取消:
- google支付回调验证
原文链接: https://my.oschina.net/lemonzone2010/blog/398736 Google支付问题 20150218,挂机的日本服务器出现google支付被刷单现象,虽 ...