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.
The premise is thatAngular cli
is installed. Most of the following file creation depends oncli
Provided directives
Angular
The 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.ts
routing file
2. At this time,CLI
will create another folderarticle
,article
file under theapp
folder The folder contains two filesarticle.module.ts
andarticle-routing.module.ts
3. Useng g component
to generate For two components, the specified template isarticle
. The specified template will be automatically imported intoarticle.modules.ts
and registered in thedeclarations
array,Note: Do not delete the components registered indeclarations
, otherwise some specifications in the components will become unusable
ng g component article/article-list -m=article
, generatearticle-list
folder component
folder ##ng g component article/article-create -m=article, generate
article-createfolder component
## under the
article
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@NgModule
decorator; the second one importsCommonModule
, which provides many functions likengIf
andngFor
Such common instructions. Feature modules importCommonModule
, notBrowserModule
, which should only be imported once in the root module.CommonModule
only contains information about commonly used directives, such asngIf
andngFor
, which are used in most templates, whileBrowserModule
Application configurations made for the browser are only used once.
Related recommendations: "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 { }
, routing address nested configuration, here The address setting is because the routing prefix of the current module has been set toarticle
in theapp-routing.module
root routing module, so the following routes can only be set directly. Visit with the routing prefix set by the root route.
list
, and the access address needs to bearticle/list
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 {}
, import theapp-routing.module
file to configure global routing
7. The routing of the root moduleimport { 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 { }
loadChildren
uses the lazy loading feature module. By default,NgModule
is 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 loadingNgModule
on demand. Lazy loading reduces the initial bundle size and therefore load time.
8. Finally, if you want to access theimport { 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 {}
andcreate
routes belowarticle
, enter the addressarticle/list in the browser
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!