博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Angular] Angular Elements Intro
阅读量:5088 次
发布时间:2019-06-13

本文共 1844 字,大约阅读时间需要 6 分钟。

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

 

1. Create a new application:

ng new elementApp

 

2. Install @angular/elements package:

ng add @angular/elements --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 titleconstructor(   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 = '
'; }}

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';

 

转载于:https://www.cnblogs.com/Answer1215/p/8992242.html

你可能感兴趣的文章
Python编译错误总结
查看>>
URL编码与解码
查看>>
日常开发时遇到的一些坑(三)
查看>>
Eclipse 安装SVN插件
查看>>
深度学习
查看>>
TCP粘包问题及解决方案
查看>>
构建之法阅读笔记02
查看>>
添加按钮
查看>>
移动端页面开发适配 rem布局原理
查看>>
Ajax中文乱码问题解决方法(服务器端用servlet)
查看>>
会计电算化常考题目一
查看>>
阿里云服务器CentOS6.9安装Mysql
查看>>
剑指offer系列6:数值的整数次方
查看>>
js 过滤敏感词
查看>>
poj2752 Seek the Name, Seek the Fame
查看>>
软件开发和软件测试,我该如何选择?(蜗牛学院)
查看>>
基本封装方法
查看>>
bcb ole拖拽功能的实现
查看>>
生活大爆炸之何为光速
查看>>
bzoj 2456: mode【瞎搞】
查看>>