[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 ...
随机推荐
- FastReport.Net使用:[11]公共对象属性介绍
公共对象属性介绍 1.Left(左),Top(上),Height(高度),Width(宽度) Left和Top,用来控制对象的位置:Height和Width用来控制对象的大小. 2.Anchor(基准 ...
- 【Splay 总结】
很多年前学的splay已经忘了? BZOJ 3729 要用Splay啊哭..强制在线.. 二叉查找树 二叉排序树(Binary Sort Tree)又称二叉查找树(Binary Search Tree ...
- hihoCoder #1695 公平分队II
题目大意 Alice 和 Bob 在玩一个游戏.Alice 将 $1$ 到 $2n$ 这 $2n$ 个整数分成两组,每组 $n$ 个.Bob 从中选一组,剩下一组归 Alice.Alice 可以与 B ...
- 24.最优布线问题(kruskal算法)
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 查看运行结果 题目描述 Description 学校需要将n台计算机连接起来,不同的2台计算机之间的连接费用 ...
- [转]Android:异步处理之AsyncTask的应用(二)
2014-11-07 既然UI老人家都这么忙了,我们这些开发者肯定不能不识趣的去添乱阻塞UI线程什么的,否则UI界面万一停止响应了呢——这不是招骂的节奏么?!所以我们知道用Handler+Th ...
- Python类中self的作用
摘自论坛: self:是指向你新创建对象实例的引用,在这个地方指向你创建的Person类的实例p.当你调用Person类创建实例时,self指的就是你这个P,所以这个地方p=Person('tiany ...
- Jenkins使用jenkins-cli.jar进行远程调用时出现“ERROR: No such job 'test'”或者权限不够等问题解决(Windows)
网上最提倡的解决办法是用SSH的key进行登录,但是我发觉Linux上非常容易实现,但是Windows压根不知道在哪里设置. 原文:https://issues.jenkins-ci.org/brow ...
- Windows Embedded Compact 7网络编程概述(下)
11.1.1 Select I/O模型 在Windows CE中,Select模型是唯一被支持的I/O模型.Select I/O模型就是利用select函数对I/O进行管理. 函数select的功能在 ...
- C语言之数组中你所不在意的重要知识
#include<stdio.h> void simpleArray(); void main() { simpleArray(); } //数组的简单操作 void simpleArra ...
- 解决/dev/fb0无法打开的问题
最近要在Linux做基于frame Buffer的图形显示,不论我在独立分区的Linux FC6系统中,还是在装有Red hat9的VPC中,都无法打开/dev/fb0.从网上找了很多资料,都没能解决 ...