angular 自定义web组件:

首先创建一个名为myCustom的组件。

引入app.module:

...
import {customComponent} from ' ./myCustom.component'; @NgModule({
declarations:[AppComponent,customComponent],
entryComponents:[customComponent]
....
}) export class AppModule{}

  

全局注册:

app.component:

import { Component,Injector} from '@angular/core';
import { createCustomElement} from '@angular/elements';
import {customComponents} from './myCustom.component'; @Component({ })
export class AppComponent{
constructor(private injector:Injector){
const customElementRef=createCustomElement(createCustomElement,{injector});
customElements.define('my-test',customElementRef); }
}

Ok,这样完成了'my-test'的全局,接下来可以像是用web component那样使用它了.

进入我们的主html:

Index.html

<html>

<body>
<app-root></app-root>
<!-- 这是angular程序主引导接口 --> <my-test></my-test>
<!-- 全局注册的angular组件可以直接放在html里使用-->
</body> </html>

注意:

1. 放在app-root前面,效果是一样的。没有先后之分。

2. my-test的注入依赖,继承自app.component。你可以放心地在里面使用。

3. 作为web component 时,input不能接受变量。output不生效。。

问:

既然angular可以注册全局web component ,那么是否能结合react使用?

当然可以,接着上面的列子:

<html>

<body>
<app-root></app-root>
<!-- 这是angular程序主引导接口 --> <my-test></my-test>
<!-- 全局注册的angular组件可以直接放在html里使用-->
<div id='react-app'></div>
</body>
<!-- 引入react.js --> <script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script> <script type='text/babel'>
class ReactApp extends React.Component{
state={
showAngularComponent:false
}
render(){
const state=this.state;
return (
<div>
{state.showAngularComponent?<my-test></my-test>:null}
<button onClick={()=>this.setState({showAngularComponent:!state.showAngularComponent})}>toggle</button>
</div>
) }
} ReactDOM.render(<ReactApp/>,document.getElementById('react-app')) </script>
</html>

  完美运行。

 angular component 注册为web component 的技术。让angular的灵活性瞬间暴涨!但是那么有必要结合react使用吗?

当然是得依你的业务要求来定了,比如你的领导强行指定你使用react。

但是当你面对一些功能复杂的组件或页面开发时,react的相对低下的效率,以及大面积重复计算、缓慢的性能,可能让你非常苦恼。此时或许你能考虑下用angular便捷迅速地为你的react项目提供一个高性能且稳定的组件。

加上ng6再一次减肥,核心库如同jquery一样的大小,angular的未来,一片光明。

---

使用custom element之前:

安装webcomponents填充库:

npm install webcomponents/webcomponentsjs --save;

polyfill.ts中引入:

import '@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js'

angular custom Element 自定义web component的更多相关文章

  1. 示例可重用的web component方式组织angular应用模块

    在online web应用中,经常有这样的需求,能够让用户通过浏览器来输入代码,同时能够根据不同的代码来做语法高亮.大家已知有很多相应的javascript库来实现语法高亮的功能,比如codemirr ...

  2. Vue报错之" [Vue warn]: Unknown custom element: <wzwzihello> - did you register the component correctly? For recursive components, make sure to provide the "name" option."

    一.报错截图 [Vue warn]: Unknown custom element: <wzwzihello> - did you register the component corre ...

  3. vue报错[Vue warn]: Unknown custom element: <router-Link> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

    vue浏览器报错,如下 vue.runtime.esm.js?2b0e:619 [Vue warn]: Unknown custom element: <router-Link> - di ...

  4. web components & publish custom element & npm

    web components & publish custom element & npm https://www.webcomponents.org/publish Polymer ...

  5. [Vue warn]: Unknown custom element: <sapn> - did you register the component correctly? For recursive components, make sure to provide the "name" option. found in ---> <Evaluate> at src/views/index/

    关于vue报错: [Vue warn]: Unknown custom element: <sapn> - did you register the component correctly ...

  6. WebComponent魔法堂:深究Custom Element 之 从过去看现在

    前言  说起Custom Element那必然会想起那个相似而又以失败告终的HTML Component.HTML Component是在IE5开始引入的新技术,用于对原生元素作功能"增强& ...

  7. Web Component探索

    概述 各种网站往往需要一些相同的模块,比如日历.调色板等等,这种模块就被称为“组件”(component).Web Component就是网页组件式开发的技术规范. 采用组件进行网站开发,有很多优点. ...

  8. WebComponent魔法堂:深究Custom Element 之 标准构建

    前言  通过<WebComponent魔法堂:深究Custom Element 之 面向痛点编程>,我们明白到其实Custom Element并不是什么新东西,我们甚至可以在IE5.5上定 ...

  9. Web Component

    前言 Web Component不是新东西,几年前的技术,但是受限于浏览器兼容性,一直没有大规模应用在项目里,直到现在(2018年年末),除IE仍不支持之外,其它主流浏览器都支持Web Compone ...

随机推荐

  1. 短URL DH 密钥交换算法

    w  追问:0-短URL 的时效性,(比如微信个人账户的永久二维码和群的约7天时效二维码):1-0中的时效性对于算法选择的影响,比如简单的HAS映射.sha1.md5......   https:// ...

  2. php accumulation rockmongo

    php -r 'echo substr(sprintf("%o",fileperms("./")),-4);'

  3. 接口测试工具 — jmeter(header与cookie的添加)

    1.header的添加 添加HTTP信息头管理器 填写header 2.添加cookie 添加HTTP Cookie管理器 添加cookie值

  4. IO 包中的其他类

    打印流 PrintWriter 和 PrintWriter 直接操作输入流和文件 序列流 SequenceInputStream 对多个输入流进行合并 操作对象 ObjectInputStream 和 ...

  5. 【pip】【conda】

    1.指定python包安裝版本==  指定python包安裝源-i: pip2 -i https://pypi.tuna.tsinghua.edu.cn/simple 2.配置文件换源 vi ~/.p ...

  6. 洛谷 P4451 [国家集训队]整数的lqp拆分

    洛谷 这个题目是黑题,本来想打表的,但是表调不出来(我逊毙了)! 然后随便打了一个递推,凑出了样例, 竟然. 竟然.. 竟然... A了!!!!!!! 直接:\(f[i]=f[i-1]*2+f[i-2 ...

  7. Java 连接池的工作原理(转)

    原文:Java 连接池的工作原理 什么是连接? 连接,是我们的编程语言与数据库交互的一种方式.我们经常会听到这么一句话“数据库连接很昂贵“. 有人接受这种说法,却不知道它的真正含义.因此,下面我将解释 ...

  8. Python 实现获取【昨天】【今天】【明天】日期

    昨天 from datetime import date, timedelta yesterday = (date.today() + timedelta(days=-1)).strftime(&qu ...

  9. django--admin模型层

    django amdin是django提供的一个后台管理页面,改管理页面提供完善的html和css,使得你在通过Model创建完数据库表之后,就可以对数据进行增删改查,而使用django admin ...

  10. Android图片加载框架Picasso最全使用教程2

    前言 前面我们已经介绍了Picasso的基本用法及如何将一张图片加载到ImageView中,下面我们就利用Picasso在ListView中加载图片;Let’s Go! 一个ListView的简单应用 ...