angular 自定义组件_如何创建Angular 6自定义元素和Web组件
angular 自定义组件
by Prateek Mishra
通过Prateek Mishra
如何创建Angular 6自定义元素和Web组件 (How to create Angular 6 Custom Elements and Web Components)
使用Angular CLI创建Angular元素的6个步骤 (6 Steps to create Angular elements using Angular CLI)
Angular elements are Angular components that carry the minified version of the whole framework. They allow you to create custom elements (one of the web components) in a framework-agnostic way. They can be used in simple web projects but with the powers of Angular within.
Angular元素是Angular组件,带有整个框架的缩小版本。 它们允许您以与框架无关的方式创建自定义元素 (Web组件之一)。 它们可以用于简单的Web项目中,但可以使用Angular的功能。
After reading the official documentation for Angular elements I realized that it lacks the implementation part in a structured manner. That is the reason I am stating the steps to get going!
阅读有关Angular元素的官方文档后 ,我意识到它缺乏结构化的实现部分。 这就是我要说明要采取的步骤的原因!
1.安装Angular CLI 6并创建一个新项目 (1. Install Angular CLI 6 and create a new project)
npm i -g @angular/cling new angular-custom-elements
As Angular introduced the concept of custom elements in Angular 6 we must have v6 or later installed. You can also add the--style
flag to set the default style extension.
当Angular在Angular 6中引入自定义元素的概念时,我们必须安装v6或更高版本。 您还可以添加--style
标志以设置默认样式扩展名。
2.添加元素包 (2. Add elements package)
Custom elements are not completely implemented by all the browsers. Hence we require Polyfills to get them working. With the new CLI command ng add
you can add the Angular library and the required polyfills:
并非所有浏览器都完全实现自定义元素。 因此,我们要求Polyfills使它们起作用。 使用新的CLI命令ng add
您可以添加Angular库和所需的polyfills:
ng add @angular/elements
3.创建一个组件 (3. Create a component)
Let’s create a component that will act as a custom element by the end of this post:
让我们创建一个在本文结尾处将用作自定义元素的组件:
ng g component button --inline-style --inline-template -v Native
We are using ViewEncapsulation.Native
to prevent the styles of component from bleeding out and affect other elements. This will render our component in browser’s native implementation of shadow DOM (v0; for v1 we use ViewEncapsulation.ShadowDOM
) by bundling all the styles, template and component class code in a single file.
我们使用ViewEncapsulation.Native
来防止组件样式ViewEncapsulation.Native
并影响其他元素。 通过将所有样式,模板和组件类代码捆绑在一个文件中,这将使我们的组件呈现在浏览器的本机影子DOM实现中(v0;对于v1,我们使用ViewEncapsulation.ShadowDOM
)。
4.向组件添加属性 (4. Add properties to the component)
After making a few changes our button component looks like:
进行一些更改后,我们的按钮组件如下所示:
According to the official docs:
根据官方文档:
The creation API parses the component looking for input properties, and defines corresponding attributes for the custom element.
创建API解析组件以查找输入属性,并为custom元素定义相应的属性。
The creation API parses the component looking for input properties, and defines corresponding attributes for the custom element.
创建API解析组件以查找输入属性,并为custom元素定义相应的属性。
It transforms the property names to make them compatible with custom elements, which do not recognize case distinctions. The resulting attribute names use dash-separated lowercase. For example, for a component with
@Input('myInputProp') inputProp
, the corresponding custom element defines an attribute ‘‘my-input-prop”
.它将转换属性名称,使其与不区分大小写的自定义元素兼容。 结果属性名称使用短划线分隔的小写字母。 例如,对于具有
@Input('myInputProp') inputProp
,相应的自定义元素定义属性“my-input-prop”
。
And also:
并且:
Component outputs are dispatched as HTML Custom Events, with the name of the custom event matching the output name.
组件输出作为HTML Custom Events调度,其自定义事件的名称与输出名称匹配。
Component outputs are dispatched as HTML Custom Events, with the name of the custom event matching the output name.
组件输出作为HTML Custom Events调度,其自定义事件的名称与输出名称匹配。
For example, for a component with
@Output() valueChanged = new EventEmitter()
, the corresponding custom element will dispatch events with the name "valueChanged". The emitted data will be stored on the event’sdetail
property. If you provide an alias, that value is used. For example,@Output('myClick') clicks = new EventEmitter<string&
gt;(); results in dispatch events with the name "myClick".例如,对于具有
@Output() valueChanged = new EventEmitter()
,相应的自定义元素将调度名称为“ valueChanged”的事件。 发出的数据将存储在事件的detail
属性中。 如果提供别名,则使用该值。 例如,@Output('myClick') clicks = new EventEmitter<string&
gt;(); 导致名称为“ myClick”的调度事件。
5.更新NgModule (5. Update NgModule)
Following are the major steps that need to be followed in app.module.ts
:
以下是app.module.ts
中需要遵循的主要步骤:
Remove the default
bootstrap
array which is set toAppComponent
删除设置为
AppComponent
的默认bootstrap
数组Since our
ButtonComponent
is not a part of any other component, and is also not a root of an Angular application, we need to tell Angular to compile it specifically. For this we put it on theentryComponents
list. Otherwise Angular tree shaking will drop this component from the prod bundle.由于我们的
ButtonComponent
不是任何其他组件的一部分,也不是Angular应用程序的根,因此我们需要告诉Angular专门对其进行编译。 为此,我们将其放在entryComponents
列表中。 否则,角度树摇晃会使该组件从产品束中掉落。Add
ngDoBootstrap()
to tell Angular to use this module for bootstrapping.添加
ngDoBootstrap()
告诉Angular使用该模块进行引导。Angular provides the
createCustomElement()
function for converting an Angular component, together with its dependencies, to a custom element. ThecreateCustomElement()
function is expecting to get two parameter:Angular提供了
createCustomElement ()
函数,用于将Angular组件及其依赖项转换为自定义元素。createCustomElement()
函数期望获得两个参数:
- First, the Angular component which should be used to create the element.首先,应使用Angular组件创建元素。
- Second, a configuration object. This object needs to include the injector property which is set to the current Injector instance.第二,配置对象。 该对象需要包括设置为当前Injector实例的注射器属性。
5. The next step is to register the newly created custom element in the browser. This is done by calling customElements.define()
. Please note that this is not Angular. The customElements read-only property belongs to the Window interface. It returns a reference to the CustomElementRegistry object. This object can be used to register new custom elements. It can also get information about previously registered custom elements in the browser.
5.下一步是在浏览器中注册新创建的自定义元素。 这是通过调用customElements.define()
。 请注意,这不是Angular。 customElements只读属性属于Window接口。 它返回对CustomElementRegistry对象的引用。 该对象可用于注册新的自定义元素。 它还可以获取有关浏览器中先前注册的自定义元素的信息。
The customElements.define()
method needs two parameter:
customElements.define()
方法需要两个参数:
The first parameter is of type string and contains the name of the element. Passing the string ‘app-button’ means that the custom element <app-button> will be registered and can be used in the HTML code.
第一个参数的类型为字符串,并包含元素的名称。 传递字符串' app-button'意味着自定义元素<app-butt on>将被注册并可以在HTML代码中使用。
- The second parameter is the custom element which has been created before.第二个参数是之前创建的自定义元素。
6. Now replace target
value in tsconfig.json
from es5
to es2015
as in browsers that support Custom Elements natively, the specification requires developers to use ES2015 classes to define Custom Elements.
6.现在替换target
在值tsconfig.json
从es5
到es2015
在浏览器的支持自定义元素本身,规范要求开发者使用ES2015类来定义自定义元素。
6.构建并运行 (6. Build and run)
In order to build we will use a standard ng build
command. But since it outputs four files (runtime.js
, scripts.js
, polyfills.js
and main.js
) and we’d like to distribute our component as a single js file, we need to turn hashing file names off. Let’s modify the scripts
in package.json
and add package
entry:
为了构建,我们将使用标准的ng build
命令。 但由于它输出四个文件( runtime.js
, scripts.js
, polyfills.js
和main.js
),我们希望我们的分发组件作为一个单一的js文件,我们需要把散列文件名了。 让我们修改package.json
的scripts
并添加package
条目:
"scripts": {…,"build": "ng build --prod --output-hashing=none",// For Windows:
"package": "jscat ./dist/angular-custom-elements/runtime.js ./dist/angular-custom-elements/polyfills.js ./dist/angular-custom-elements/scripts.js ./dist/angular-custom-elements/main.js > custom-button-element.js",// For Mac or Linux:
"package": "cat ./dist/angular-custom-elements/runtime.js ./dist/angular-custom-elements/polyfills.js ./dist/angular-custom-elements/scripts.js ./dist/angular-custom-elements/main.js > custom-button-element.js",…,}
Since Windows OS has no cat
command run npm i jscat
.
由于Windows操作系统没有cat
命令,请运行npm i jscat
。
Save all and finally run:
保存所有并最终运行:
npm run build && npm run package
The command generates custom-button-element.js
that you can include in <script>
of an HTML page to see our custom element working.
该命令会生成custom-button-element.js
,您可以将其包含在HTML页面的<script>
,以查看我们的自定义元素是否有效。
Here is an example:
这是一个例子:
摘要 (Summary)
In summary we’ve:
总而言之,我们已经:
- Added important libraries for implementation添加了重要的实现库
- Registered the component in browser’s CustomElementRegistry在浏览器的CustomElementRegistry中注册了组件
- Combined the build artifacts to a single file将构建工件组合到单个文件中
Complete source code can be found here.
完整的源代码可以在这里找到。
Did you learn something new? If so please clap ? button below️ so more people can see this.
你学到新东西了吗? 如果是这样,请拍手? 下方的按钮 ️,以便更多人可以看到。
翻译自: https://www.freecodecamp.org/news/how-to-create-angular-6-custom-elements-web-components-c88814dc6e0a/
angular 自定义组件
angular 自定义组件_如何创建Angular 6自定义元素和Web组件相关推荐
- vue自定义音频播放组件_易于创建Vue的自定义音频播放器组件
vue自定义音频播放组件 音频更好 (vue-audio-better) Easy to create custom audio player components for Vue.js. 易于为Vu ...
- 自定义搜索引擎_如何创建自己的自定义Google搜索引擎
自定义搜索引擎 Have you ever wanted to create a custom Google search engine that searches only specific web ...
- pb怎么封装com组件_从零开始构建 Angular 组件库
NG-ZORRO 组件库官网地址:Ant Design Of Angular Github地址:NG-ZORRO/ng-zorro-antd 更新:视频已上传 谢亚东演讲视频_腾讯视频v.qq.co ...
- angular过滤字符_如何使用Angular和Azure计算机视觉创建光学字符读取器
angular过滤字符 介绍 (Introduction) In this article, we will create an optical character recognition (OCR) ...
- 4 angular 重构 项目_再遇angular(angular4项目实战指南)
这两天看了看angular4的文档,发现他和angular1.X的差别真的是太大了,官方给出的那个管理英雄的Demo是一个非常好的入门项目,这里给出一个管理个人计划的小项目,从头至尾一步一步讲解如何去 ...
- angular 模块构建_如何使用Angular和服务人员构建无需Internet即可运行的网站
angular 模块构建 by Tomiwa 通过Tomiwa 如何使用Angular和服务人员构建无需Internet即可运行的网站 (How to build websites that work ...
- angular 手动注入_手动引导Angular JS应用程序
angular 手动注入 Earlier we looked at various angular form features and its validation. In this post, We ...
- computed set 自定义参数_深入理解vmodel之自定义组件用法
根据上一篇<深入理解 v-model 之表单用法>基本对 v-model 有了比较深的理解,接下来我们看看它如何在自定义组件中使用. 首先,我们知道下面两个用法等价的: <input ...
- python如何导入自定义模块_【python】导入自定义模块
一.直接import 1.当执行文件与要导入的py文件在同一目录下时 假设要在wangyi.py中导入weibo.py文件 import weibo 2.当执行文件与要导入的py文件所在文件夹在同一目 ...
最新文章
- 过椭圆外一点引两条切线方程_椭圆的一些结论汇总
- MySQL 获得当前日期时间 函数
- Ubuntu配置远程访问的xrdp协议和teamviewer软件
- 用jedis访问Redis进行对象存取示例
- 学成在线--16.添加课程计划
- 数据结构_链表_单向链表
- ArcGIS 10.2数字化线状要素时自己主动拼接成一条线
- C++中如何判断文件是否存在
- Android-JNI开发系列《七》补充jni与java的数据类型的对应关系和数据类型描述符
- async function
- 做跨境不知道选什么类目,看看这些常青树类目
- MA、BMA、PPP网络类型实验
- oracle 结果集已耗尽_结果集已耗尽
- k8s-重启kubelet服务异常 kubelet.service: main process exited, code=exited, status=1/FAILURE
- android知乎小圆圈刷新效果
- 大话PM|产品经理入门概念通识
- 伯明翰大学计算机科学怎么样,独家解析!伯明翰大学的优势专业:工程/计算机科学...
- 外壳IK碰撞冲击测试
- RenderDoc[01] 使用RenderDoc 分析Android游戏(免Root)
- LE5010蓝牙芯片(凌思微)开发总结