A brief analysis of what is ngModule in Angular
What is ngModule? This article will give you a brief understanding of angular syntax and introduce ngModule in Angular. I hope it will be helpful to you!
As an Angular10 tutorial, in my understanding, compared to VUE, angular is better modularized, which makes the code structure appear clearer. So in this section, we will briefly introduce angular syntax and understanding of ngModule. [Related tutorial recommendations: "angular tutorial"]
First of all, what is angular module (ngModule)?
My understanding: In fact, it is Ordinary classes decorated with @NgModule, nothing special.
Then the question is, what is @NgModule?
Let’s first take a look at the default code in src/app/app.module.ts:
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; // @NgModule(元数据) @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, AppRoutingModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { } // 模块名AppModule
@NgModule gets a metadata object, which will tell Angular how to compile and start this application. (Metadata is more than the above configuration items, but let’s talk about these first)
declarations array
The word itself means "announcement, announcement" , here are the dependencies of this module. Including some components, directives and pipelines that the module may depend on. Import rules:
- To use them, they must be imported first. \
- A component, directive or pipe can only be introduced (declared) by one module
- The components in declarations can only be used in the current module by default. If you want other modules to use them, they must exports out
imports array
The imports array will only appear in the @NgModule decorator. A module wants to work normally, except for its own dependencies ( declarations) and may also require dependencies exported by other modules. As long as it is an angular module, it can be imported into the imports array, such as custom modules (such as AppRoutingModule), third parties or ng built-in ones (@angular/**)
providers array
Provide a series of services
bootstrap array
Each component in the array is used as a component tree Root (root component), during the startup process, the components inside will be parsed one by one and inserted into the browser's DOM.
But usually, there is only one AppComponent inside.
What does the angular component look like?
Look at the app component first, the default code of src/app/app.component.ts
:
import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'] }) export class AppComponent {// 组件名AppComponent title = 'hero'; }
It can be seen that it is still a normal operation: Introduction–>Configuration–>Export
##selector:
As the name suggests, it is a choice It is just a selector that can be selected through native JS (required configuration).templateUrl:
The URL of the Angular component template file. If it is provided, do not usetemplate to provide inline templates (choose one of the
templateUrl and
template options, required configuration).
styleUrls:
It is not difficult to see that it is theconfiguration (optional configuration) that introduces one or more style paths
If the component is relatively simple, we don’t need to extract the page and style separately. The configuration items of @Component can be directly used inline form:
import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: ` <h1>{{title}}</h1> <h2>My favorite hero is: {{myHero}}</h2> `, styles: [`h1 { color: red }`] }) export class AppComponent { // 组件名AppComponent title = 'Tour of Heroes'; myHero = 'Windstorm'; }
The general idea of angular application should be like this:
For more programming-related knowledge, please visit:For example, if an angular application is a company, then AppModule This is the company. AppComponent is a factory of this company, and a company can have many factories. The elements in the declearation array are parts of the factory, such as production workshops, personnel management systems, etc. The imports array is like foreign aid invited by the factory, and it is relatively professional. The providers array is like a logistics department that provides various services.
Introduction to Programming! !
The above is the detailed content of A brief analysis of what is ngModule in Angular. 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

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

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











This article continues the learning of Angular, takes you to understand the metadata and decorators in Angular, and briefly understands their usage. I hope it will be helpful to everyone!

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

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

This article will give you an in-depth understanding of Angular's state manager NgRx and introduce how to use NgRx. I hope it will be helpful to you!

How to use monaco-editor in angular? The following article records the use of monaco-editor in angular that was used in a recent business. I hope it will be helpful to everyone!

This article will share with you an Angular practical experience and learn how to quickly develop a backend system using angualr combined with ng-zorro. I hope it will be helpful to everyone!

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

Authentication is one of the most important parts of any web application. This tutorial discusses token-based authentication systems and how they differ from traditional login systems. By the end of this tutorial, you will see a fully working demo written in Angular and Node.js. Traditional Authentication Systems Before moving on to token-based authentication systems, let’s take a look at traditional authentication systems. The user provides their username and password in the login form and clicks Login. After making the request, authenticate the user on the backend by querying the database. If the request is valid, a session is created using the user information obtained from the database, and the session information is returned in the response header so that the session ID is stored in the browser. Provides access to applications subject to
