Analyzing the source code examples of Angular Component
This article mainly introduces the source code examples of analyzing Angular Component. Now I share it with you and give you a reference.
Web Component
Before introducing Angular Component, let’s briefly understand W3C Web Components
Definition
W3C proposes the standard of Web Component to unify the standard way of componentization.
Each component contains its own html, css, and js code.
Web Component standard includes the following four important concepts:
1.Custom Elements (custom tags): You can create custom HTML tags and elements;
2.HTML Templates (HTML templates): use < ;template> tag to predefine some content, but it does not load it into the page, but uses JS code to initialize it;
3.Shadow DOM (virtual DOM): You can create a DOM subtree that is completely independent from other elements;
4.HTML Imports: A method of introducing other HTML documents into HTML documents, .
In summary, the ability to create custom tags to introduce components is the basis for front-end componentization. References to HTML files and HTML templates on the page are used to support writing component views and component resource management, while Shadow DOM It is to isolate the conflicts and impacts of code between components.
Example
Define hello-component
<template id="hello-template"> <style> h1 { color: red; } </style> <h1>Hello Web Component!</h1> </template> <script> // 指向导入文档,即本例的index.html var indexDoc = document; // 指向被导入文档,即当前文档hello.html var helloDoc = (indexDoc._currentScript || indexDoc.currentScript).ownerDocument; // 获得上面的模板 var tmpl = helloDoc.querySelector('#hello-template'); // 创建一个新元素的原型,继承自HTMLElement var HelloProto = Object.create(HTMLElement.prototype); // 设置 Shadow DOM 并将模板的内容克隆进去 HelloProto.createdCallback = function() { var root = this.createShadowRoot(); root.appendChild(indexDoc.importNode(tmpl.content, true)); }; // 注册新元素 var hello = indexDoc.registerElement('hello-component', { prototype: HelloProto }); </script>
Use hello-component
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-COMPATIBLE" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="author" content="赖祥燃, laixiangran@163.com, http://www.laixiangran.cn"/> <title>Web Component</title> <!--导入自定义组件--> <link rel="import" href="hello.html" rel="external nofollow" > </head> <body> <!--自定义标签--> <hello-component></hello-component> </body> </html>
As you can see from the above code, hello.html is a component defined according to standards (named hello-component). This component has its own structure, style and logic, and then introduce the component file in index.html, and it can be used like a normal tag.
Angular Component
Angular Component is a type of directive and can be understood as a directive with a template. The other two types are attribute directives and structural directives.
Basic composition
@Component({ selector: 'demo-component', template: 'Demo Component' }) export class DemoComponent {}
- ##Component decorator: Each component class must be decorated with @component To become an Angular component.
- Component metadata: Component metadata: selector, template, etc. The following will focus on the meaning of each metadata.
- Component class: Component is actually an ordinary class, and the logic of the component is defined and implemented in the component class.
- Component template: Each component will be associated with a template, which will eventually be rendered on the page. The DOM element on the page is the host element of this component instance.
Type | Function | |
---|---|---|
AnimationEntryMetadata[] | Setting component Animation | |
ChangeDetectionStrategy | Set the component’s change detection strategy | |
ViewEncapsulation | Set the component's view packaging options | |
any[] | The settings will be dynamically inserted into Component list in this component view | |
[string, string] | Interpolation mark of custom component, the default is double curly brackets | |
string | Set the module id of the component under the ES/CommonJS specification, which is used to resolve the relative path of template styles | |
string[] | Set the external style file referenced by the component | |
string[] | Set the inline style used by the component | |
string | Set the inline template of the component | |
string | Set the path to the component template | |
Provider[] | Sets the services available to the component and all its subcomponents (excluding ContentChildren) |
Type | Function | |
---|---|---|
string | Set the alias of the component instance in the template so that it can be called in the template | |
{ [key: string]: string} | Set the events, actions and properties of the component | |
string[] | Set the input properties of the component | |
string[] | Set the output properties of the component | |
Provider[] | Set the services (dependency injection) available to the component and all its subcomponents (including ContentChildren) | |
{[key: string]: any} | Set the query that needs to be injected into the component | |
string | Set the css selector (custom label of the component) used to identify the component in the template |
生命周期钩子 | 调用时机 |
---|---|
ngOnChanges | 在ngOnInit之前调用,或者当组件输入数据(通过@Input装饰器显式指定的那些变量)变化时调用。 |
ngOnInit | 第一次ngOnChanges之后调用。建议此时获取数据,不要在构造函数中获取。 |
ngDoCheck | 每次变化监测发生时被调用。 |
ngAfterContentInit | 使用 |
ngAfterContentChecked | ngAfterContentInit后被调用,或者每次变化监测发生时被调用(只适用组件)。 |
ngAfterViewInit | 创建了组件的视图及其子视图之后被调用(只适用组件)。 |
ngAfterViewChecked | ngAfterViewInit,或者每次子组件变化监测时被调用(只适用组件)。 |
ngOnDestroy | 销毁指令/组件之前触发。此时应将不会被垃圾回收器自动回收的资源(比如已订阅的观察者事件、绑定过的DOM事件、通过setTimeout或setInterval设置过的计时器等等)手动销毁掉。 |
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
The above is the detailed content of Analyzing the source code examples of Angular Component. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub

Introduction to Python functions: Introduction and examples of exec function Introduction: In Python, exec is a built-in function that is used to execute Python code stored in a string or file. The exec function provides a way to dynamically execute code, allowing the program to generate, modify, and execute code as needed during runtime. This article will introduce how to use the exec function and give some practical code examples. How to use the exec function: The basic syntax of the exec function is as follows: exec

Indentation specifications and examples of Go language Go language is a programming language developed by Google. It is known for its concise and clear syntax, in which indentation specifications play a crucial role in the readability and beauty of the code. effect. This article will introduce the indentation specifications of the Go language and explain in detail through specific code examples. Indentation specifications In the Go language, tabs are used for indentation instead of spaces. Each level of indentation is one tab, usually set to a width of 4 spaces. Such specifications unify the coding style and enable teams to work together to compile

The DECODE function in Oracle is a conditional expression that is often used to return different results based on different conditions in query statements. This article will introduce the syntax, usage and sample code of the DECODE function in detail. 1. DECODE function syntax DECODE(expr,search1,result1[,search2,result2,...,default]) expr: the expression or field to be compared. search1,

Introduction to Python functions: usage and examples of the abs function 1. Introduction to the usage of the abs function In Python, the abs function is a built-in function used to calculate the absolute value of a given value. It can accept a numeric argument and return the absolute value of that number. The basic syntax of the abs function is as follows: abs(x) where x is the numerical parameter to calculate the absolute value, which can be an integer or a floating point number. 2. Examples of abs function Below we will show the usage of abs function through some specific examples: Example 1: Calculation

Introduction to Python functions: Usage and examples of the isinstance function Python is a powerful programming language that provides many built-in functions to make programming more convenient and efficient. One of the very useful built-in functions is the isinstance() function. This article will introduce the usage and examples of the isinstance function and provide specific code examples. The isinstance() function is used to determine whether an object is an instance of a specified class or type. The syntax of this function is as follows

The default display behavior for components in the Angular framework is not for block-level elements. This design choice promotes encapsulation of component styles and encourages developers to consciously define how each component is displayed. By explicitly setting the CSS property display, the display of Angular components can be fully controlled to achieve the desired layout and responsiveness.

Calling the @Bean annotated method in the @Configuration class returns the same example; calling the @Bean annotated method in the @Component class returns a new instance.
