Table of Contents
2. Create Component
3. View encapsulation mode
4. 总结
Home Web Front-end JS Tutorial A brief discussion on the basic knowledge of components (@Component) in Angular

A brief discussion on the basic knowledge of components (@Component) in Angular

Jun 11, 2021 am 10:44 AM
angular components

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.

A brief discussion on the basic knowledge of components (@Component) in Angular

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>
Copy after login

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-name >.component.ts (e.g my-component.component.ts)
  • A template file < component-name >.component.html (e.g my-component.component.html)
  • A CSS file, < component-name >.component.css (e.g my-component.component.css)
  • Test file < component-name >.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-name >.

@Component({
  selector: &#39;app-my-component&#39;,
  templateUrl: &#39;./my-component.component.html&#39;,
  styleUrls: [&#39;./my-component.component.css&#39;]
})
export class MyComponentComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }

}
Copy after login

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: &#39;app-component-overview&#39;,
  templateUrl: &#39;./component-overview.component.html&#39;,
})
Copy after login

2. html code method

@Component({
  selector: &#39;app-component-overview&#39;,
  template: &#39;<h1>Hello World!</h1>&#39;,
})
Copy after login

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: &#39;app-my-component&#39;,
  templateUrl: &#39;./my-component.component.html&#39;,
  encapsulation: ViewEncapsulation.None,
  styleUrls: [&#39;./my-component.component.css&#39;]
})
Copy after login

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:

  1. 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)
  2. 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.)
  3. 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 {
}
Copy after login

3.2. inline-styles

默认AngularCLI生成的component,css在一个单独文件中。当然,同html模板类似,如果需要,你也可以制定css样式写在ts中, 不单独放到一个文件中。命令:ng generate component MyComponent --inline-style

生成component如下

@Component({
  selector: &#39;app-my-component&#39;,
  template: &#39;<h1>Hello World!</h1>&#39;,
  styles: [&#39;h1 { font-weight: normal; }&#39;]
})
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to install the Windows 10 old version component DirectPlay How to install the Windows 10 old version component DirectPlay Dec 28, 2023 pm 03:43 PM

Many users always encounter some problems when playing some games on win10, such as screen freezes and blurred screens. At this time, we can solve the problem by turning on the directplay function, and the operation method of the function is also Very simple. How to install directplay, the old component of win10 1. Enter "Control Panel" in the search box and open it 2. Select large icons as the viewing method 3. Find "Programs and Features" 4. Click on the left to enable or turn off win functions 5. Select the old version here Just check the box

How to install Angular on Ubuntu 24.04 How to install Angular on Ubuntu 24.04 Mar 23, 2024 pm 12:20 PM

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

An article exploring server-side rendering (SSR) in Angular An article exploring server-side rendering (SSR) in Angular Dec 27, 2022 pm 07:24 PM

Do you know Angular Universal? It can help the website provide better SEO support!

How to use PHP and Angular for front-end development How to use PHP and Angular for front-end development May 11, 2023 pm 04:04 PM

With the rapid development of the Internet, front-end development technology is also constantly improving and iterating. PHP and Angular are two technologies widely used in front-end development. PHP is a server-side scripting language that can handle tasks such as processing forms, generating dynamic pages, and managing access permissions. Angular is a JavaScript framework that can be used to develop single-page applications and build componentized web applications. This article will introduce how to use PHP and Angular for front-end development, and how to combine them

How to implement calendar component using Vue? How to implement calendar component using Vue? Jun 25, 2023 pm 01:28 PM

Vue is a very popular front-end framework. It provides many tools and functions, such as componentization, data binding, event handling, etc., which can help developers build efficient, flexible and easy-to-maintain Web applications. In this article, I will introduce how to implement a calendar component using Vue. 1. Requirements analysis First, we need to analyze the requirements of this calendar component. A basic calendar should have the following functions: display the calendar page of the current month; support switching to the previous month or next month; support clicking on a certain day,

VUE3 development basics: using extends to inherit components VUE3 development basics: using extends to inherit components Jun 16, 2023 am 08:58 AM

Vue is one of the most popular front-end frameworks currently, and VUE3 is the latest version of the Vue framework. Compared with VUE2, VUE3 has higher performance and a better development experience, and has become the first choice of many developers. In VUE3, using extends to inherit components is a very practical development method. This article will introduce how to use extends to inherit components. What is extends? In Vue, extends is a very practical attribute, which can be used for child components to inherit from their parents.

Angular components and their display properties: understanding non-block default values Angular components and their display properties: understanding non-block default values Mar 15, 2024 pm 04:51 PM

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.

How to open the settings of the old version of win10 components How to open the settings of the old version of win10 components Dec 22, 2023 am 08:45 AM

Win10 old version components need to be turned on by users themselves in the settings, because many components are usually closed by default. First we need to enter the settings. The operation is very simple. Just follow the steps below. Where are the win10 old version components? Open 1. Click Start, then click "Win System" 2. Click to enter the Control Panel 3. Then click the program below 4. Click "Enable or turn off Win functions" 5. Here you can choose what you want to open

See all articles