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的更多相关文章

  1. [Angular 2] Directive intro and exportAs

    First, What is directive, what is the difference between component and directive. For my understandi ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. Angular - - angular.Module

    angular.Module Angular模块配置接口. 方法: provider(name,providerType); name:服务名称. providerType:创建一个服务的实例的构造函 ...

  6. Angular - - angular.injector、angular.module

    angular.injector 创建一个injector对象, 调用injector对象的方法可用于获取服务以及依赖注入. 格式:angular.injector(modules); modules ...

  7. Angular - - Angular数据类型判断

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

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

    angular.uppercase 将指定的字符串转换成大写 格式:angular.uppercase(string); string:被转换成大写的字符串. 使用代码: var str = &quo ...

  9. Angular - - angular.bind、angular.bootstrap、angular.copy

    angular.bind 返回一个调用self的函数fn(self代表fn里的this).可以给fn提供参数args(*).这个功能也被称为局部操作,以区别功能. 格式:angular.bind(se ...

随机推荐

  1. data:image/png;base64这什么玩意

    周末下载了个开源的cms系统,基于java. jeecms 这是官网链接 后台页面采用VUE技术全面进行了改版 我勒个去,啥玩意,无非就是js的框架罢了.vue文件 之后再后台管理页面调试的时候发现了 ...

  2. ajax个人总结

    ajax是什么? AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 不是新的编程语言,而是一种使用现有标准的新方法. ...

  3. margin padding 图

  4. Python中__new__()方法的使用和实例化

    new()是在新式类中新出现的方法,它作用在构造方法init()建造实例之前,可以这么理解,在Python 中存在于类里面的构造方法init()负责将类的实例化,而在init()调用之前,new()决 ...

  5. 安卓 onTouch OnTouchEvent onChick 顺序

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 分发触摸事件 -> 在 触摸 时候 -> 在触摸事件时候->在点击时候 ...

  6. mvc 从客户端 中检测到有潜在危险的 Request.Form 值

    天往MVC中加入了一个富文本编辑框,在提交信息的时候报了如下的错误:从客户端(Content="<EM ><STRONG ><U >这是测试这...&qu ...

  7. request.getScheme() 取到https正确的协议(转载)

    最近在做一个项目, 架构上使用了 Nginx +tomcat 集群, 且nginx下配置了SSL,tomcat no SSL,项目使用https协议 但是,明明是https url请求,发现 log里 ...

  8. [转]Android应用中返回键的监听及处理

    用户在点击手机的返回按钮时,默认是推出当前的activty,但是有时用户不小心按到返回,所以需要给用户一个提示,这就需要重写onkeydown事件,实现的效果如下:   标签:    Android ...

  9. VK Cup 2016 - Round 1 (Div. 2 Edition) D. Bear and Polynomials

    D. Bear and Polynomials 题目连接: http://www.codeforces.com/contest/658/problem/D Description Limak is a ...

  10. Codeforces Beta Round #5 D. Follow Traffic Rules 物理

    D. Follow Traffic Rules 题目连接: http://www.codeforces.com/contest/5/problem/D Description Everybody kn ...