Detailed explanation of Angular root module and feature module

青灯夜游
Release: 2021-03-24 11:19:54
forward
1561 people have browsed it

This article will take you through the feature templates and root templates inAngular. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Detailed explanation of Angular root module and feature module

The premise is thatAngular cliis installed. Most of the following file creation depends oncliProvided directives

AngularThe attribute template and root template (AppModule)

The difference is that feature templates can divide applications. My personal understanding is similar to componentization

1. Feature templates The created commandng g module article, here isng g module article --routing, which can generate aarticle-routing.module.tsrouting file

2. At this time,CLIwill create another folderarticle,articlefile under theappfolder The folder contains two filesarticle.module.tsandarticle-routing.module.ts

3. Useng g componentto generate For two components, the specified template isarticle. The specified template will be automatically imported intoarticle.modules.tsand registered in thedeclarationsarray,Note: Do not delete the components registered indeclarations, otherwise some specifications in the components will become unusable

  • ng g component description
  • ng g component article/article-list -m=article, generatearticle-listfolder component
  • # under the
  • article

    folder ##ng g component article/article-create -m=article, generatearticle-createfolder component## under thearticle

    folder
  • #4,
article.module.ts

In the feature module generated by the CLI, there are two JavaScript import statements at the top of the file: the first importsNgModule, which The same as in the root module allows you to use the@NgModuledecorator; the second one importsCommonModule, which provides many functions likengIfandngForSuch common instructions. Feature modules importCommonModule, notBrowserModule, which should only be imported once in the root module.CommonModuleonly contains information about commonly used directives, such asngIfandngFor, which are used in most templates, whileBrowserModuleApplication configurations made for the browser are only used once.

import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { ArticleRoutingModule } from './article-routing.module'; import { ArticleListComponent } from './article-list/article-list.component'; import { ArticleCreateComponent } from './article-create/article-create.component'; @NgModule({ declarations: [ArticleListComponent, ArticleCreateComponent], imports: [ CommonModule, ArticleRoutingModule ] }) export class ArticleModule { }
Copy after login
Related recommendations: "

angular tutorial

"5,

article-routing.module.ts

, routing address nested configuration, here The address setting is because the routing prefix of the current module has been set toarticlein theapp-routing.moduleroot routing module, so the following routes can only be set directly. Visit with the routing prefix set by the root route.

For example, the root route is set to
    article
  • , the setting here islist, and the access address needs to bearticle/listDetailed explanation of Angular root module and feature module
    import { NgModule } from '@angular/core' import { Routes, RouterModule } from '@angular/router' import { ArticleListComponent } from './article-list/article-list.component' import { ArticleCreateComponent } from './article-create/article-create.component' const routes: Routes = [ { path: '', children: [ { path: '', pathMatch:'full', redirectTo: '/article/list' }, { path: 'list', component: ArticleListComponent }, { path: 'create', component: ArticleCreateComponent } ] } ] @NgModule({ imports: [RouterModule.forChild(routes)], exports: [RouterModule] }) export class ArticleRoutingModule {}
    Copy after login
  • 6. Root module
app.mudles.ts

, import theapp-routing.modulefile to configure global routing

import { HttpClientModule } from '@angular/common/http'; import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { BrowserModule } from '@angular/platform-browser'; import { AppRoutingModule } from './app-routing.module' import { AppComponent } from './app.component'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, FormsModule, HttpClientModule, AppRoutingModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
Copy after login
7. The routing of the root module

app-routing.module.ts

loadChildrenuses the lazy loading feature module. By default,NgModuleis Urgently loaded, which means it will be loaded as soon as the application loads, this is true for all modules, regardless of whether they are needed immediately or not. For larger applications with many routes, consider using lazy loading - a pattern of loadingNgModuleon demand. Lazy loading reduces the initial bundle size and therefore load time.

import { NgModule } from '@angular/core' import { Routes, RouterModule } from '@angular/router' import { LoginComponent } from './login/login.component' const routes: Routes = [ { path: 'login', component: LoginComponent }, { path: 'article', loadChildren: () => import('./article/article.module').then((m) => m.ArticleModule) } ] @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule {}
Copy after login
8. Finally, if you want to access the

list

andcreateroutes belowarticle, enter the addressarticle/list in the browser

  • article/list

    Detailed explanation of Angular root module and feature module

    ##or
  • article /create
  • Detailed explanation of Angular root module and feature module

    For more programming-related knowledge, please visit:
  • programming video
! !

The above is the detailed content of Detailed explanation of Angular root module and feature module. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!