This article will introduce to you the root template and feature template inAngular10. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
The premise is that
Angular cli
is installed. Most of the following file creation depends onInstructions provided by cli
) in Angular
1. Instructions for creating feature templates
ng g module article, what is used here isng g module article --routing
can generate aarticle-routing.module.ts
routing file. [Related tutorial recommendations: "angular tutorial
"]2. At this time,
will create another folder under theapp
folderarticle
,article
folder contains two filesarticle.module.ts
andarticle-routing.module.ts
3. Use
to generate two components, specify the template asarticle
, and the specified template will be automatically imported intoarticle.modules.ts
, and registered to thedeclarations
array,Note: Do not delete the components registered in
declarations, otherwise some of the components specified in the component will not be available
article
folderarticle-list
Folder component
article
folderarticle-create
Folder component
In the feature module generated by the CLI, there are two JavaScript imports at the top of the file Statement: The first one importsNgModule
, which allows you to use the@NgModule
decorator just like in the root module; the second one importsCommonModule
, which Many common directives likengIf
andngFor
are provided. 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.
5,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
, the address setting here is because inapp-routing.moduleThe routing prefix of the current module has been set in the root routing module toarticle
, so the following routes only need to be set directly. When accessing, bring the routing prefix set by the root routing.
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, which can be configured Global routing
7, root module routingimport { 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 thelazy loading feature module
default In this case,NgModuleis loaded urgently, which means that it will be loaded as soon as possible when the application is loaded. This is true for all modules, 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
or
The above is the detailed content of Detailed explanation of root templates and attribute templates in Angular. For more information, please follow other related articles on the PHP Chinese website!