Table of Contents
First of all, what is angular module (ngModule)?
Then the question is, what is @NgModule?
declarations array
imports array
providers array
bootstrap array
What does the angular component look like?
##selector:
templateUrl:
styleUrls:
Home Web Front-end JS Tutorial A brief analysis of what is ngModule in Angular

A brief analysis of what is ngModule in Angular

Sep 16, 2021 am 10:21 AM
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!

A brief analysis of what is ngModule in Angular

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

@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';
}
Copy after login

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 use

template to provide inline templates (choose one of the templateUrl and template options, required configuration).

styleUrls:

It is not difficult to see that it is the

configuration (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 = &#39;Tour of Heroes&#39;;
  myHero = &#39;Windstorm&#39;;
}
Copy after login
To So far, we have actually briefly talked about the default App module. As for the app-routing.module.ts file inside, we will talk about it later.

The general idea of ​​angular application should be like this:

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.

For more programming-related knowledge, please visit:

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!

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)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
Let's talk about metadata and decorators in Angular Let's talk about metadata and decorators in Angular Feb 28, 2022 am 11:10 AM

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!

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!

Detailed explanation of angular learning state manager NgRx Detailed explanation of angular learning state manager NgRx May 25, 2022 am 11:01 AM

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!

A brief analysis of how to use monaco-editor in angular A brief analysis of how to use monaco-editor in angular Oct 17, 2022 pm 08:04 PM

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!

Angular + NG-ZORRO quickly develop a backend system Angular + NG-ZORRO quickly develop a backend system Apr 21, 2022 am 10:45 AM

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!

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

Token-based authentication with Angular and Node Token-based authentication with Angular and Node Sep 01, 2023 pm 02:01 PM

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

See all articles