


A brief discussion on the basic knowledge of components (@Component) in Angular
This article will introduce to you the basic knowledge of components (@Component) in Angular. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Environment:
- Angular CLI: 11.0.6
- Angular: 11.0.7
- Node: 12.18.3
- npm : 6.14.6
- IDE: Visual Studio Code
1. Overview
Components are the main building blocks of Angular applications. Each component includes the following parts:
- An HTML template used to declare the content to be rendered on the page
- A Typescript class used to define behavior
- A CSS Selector, used to define how the component is used in the template
- (Optional) CSS style to be applied to the template
Component can be a page or a Component (control). Always, is a page element. [Related tutorial recommendations: "angular tutorial"]
Any Component is part of NgModule, so that it can be called by other applications and other Components. In order to define a Component as a member of NgModule, developers should list the Component they developed in the declarations attribute of NgModule.
In addition, developers can configure metadata through the Component modifier (that is, @Component), so that Components can control their running environment through various Life-Cycle hooks.
2. Create Component
Based on AngularCLI, Component can be easily created. In the directory where you want to create the Component, execute the following command
ng generate component <component-name></component-name>
e.g. ng generate component MyComponent
AngularCLI will automatically generate a folder and 4 files:
- A folder named after the component (e.g my-component)
- A component file .component.ts (e.g my-component.component.ts)
- A template file .component.html (e.g my-component.component.html)
- A CSS file, .component.css (e.g my-component.component.css)
- Test file .component.spec.ts (e.g my-component.component.spec.ts)
For Component, all file names will automatically add the Component suffix, so it is not recommended to include the word 'component' in .
@Component({ selector: 'app-my-component', templateUrl: './my-component.component.html', styleUrls: ['./my-component.component.css'] }) export class MyComponentComponent implements OnInit { constructor() { } ngOnInit(): void { } }
The above is the core ts file, which specifies the selector (CSS selector), template (html) file, and css file. HTML/css files can be shared by multiple components if necessary. Especially for css, you can see that the parameter is Array, so multiple css can be formulated.
2.1. Component template
Component template, that is, the HTML part, can be an html file or an html description , are equivalent. Angular components require a template defined with template or templateUrl. But you can't have both statements in a component at the same time.
1. html file method
@Component({ selector: 'app-component-overview', templateUrl: './component-overview.component.html', })
2. html code method
@Component({ selector: 'app-component-overview', template: '<h1 id="Hello-World">Hello World!</h1>', })
3. View encapsulation mode
In Angular, the CSS style of a component is encapsulated into its own view without affecting other parts of the application. This part can also be controlled through configuration.
@Component({ selector: 'app-my-component', templateUrl: './my-component.component.html', encapsulation: ViewEncapsulation.None, styleUrls: ['./my-component.component.css'] })
You can see that a encapsulation
attribute (view encapsulation mode) has been added. You can control the encapsulation mode of each component individually by setting the view encapsulation mode on the component's metadata. The optional encapsulation modes are as follows:
- Emulated mode (default value) simulates the behavior of Shadow DOM by preprocessing (and renaming) CSS code to limit CSS styles to components purpose in view. For more information, see Appendix 1. (Note: Only in, not out, global styles can come in, but component styles cannot go out)
- ShadowDom mode uses the browser's native Shadow DOM implementation to attach a Shadow DOM to the host element of the component. The component's view is attached to this Shadow DOM, and the component's styles are also included in this Shadow DOM. (Explanation: No entry or exit, no styles can come in, and component styles cannot go out.)
- None means Angular does not use view encapsulation. Angular adds CSS to global styles. The scoping rules, isolation and protection discussed earlier will not apply. Essentially, this is the same as putting the component's styles directly into HTML.
3.1. Special selector: host
Use: host pseudo-class selector to select the component host element element (relative to the element inside the component template). The :host option is the only way to target a host element. Otherwise, you won't be able to specify it, since the host is not part of the component's own template, but part of the parent component's template.
e.g.
:host { }
3.2. inline-styles
默认AngularCLI生成的component,css在一个单独文件中。当然,同html模板类似,如果需要,你也可以制定css样式写在ts中, 不单独放到一个文件中。命令:ng generate component MyComponent --inline-style
。
生成component如下
@Component({ selector: 'app-my-component', template: '<h1 id="Hello-nbsp-World">Hello World!</h1>', styles: ['h1 { font-weight: normal; }'] })
4. 总结
- Angular CLI辅助创建一个component所需的多个文件
- 建议html/css/ts分开
- 在期望目录下执行Angular CLI命令,可以生成到制定目录
-
ng generate component XXX
可以简写为ng g c
更多编程相关知识,请访问:编程视频!!
The above is the detailed content of A brief discussion on the basic knowledge of components (@Component) in Angular. For more information, please follow other related articles on the PHP Chinese website!

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing


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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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