Detailed explanation of root templates and attribute templates in Angular

青灯夜游
Release: 2021-06-04 11:28:07
forward
1878 people have browsed it

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.

Detailed explanation of root templates and attribute templates in Angular

The premise is thatAngular cliis installed. Most of the following file creation depends onInstructions provided by cli

# Feature template and root template (AppModule) in Angular

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

1. Instructions for creating feature templates

ng g module article

, what is used here isng g module article --routingcan generate aarticle-routing.module.tsrouting file. [Related tutorial recommendations: "angular tutorial"]2. At this time,

CLI

will create another folder under theappfolderarticle,articlefolder contains two filesarticle.module.tsandarticle-routing.module.ts3. Use

ng g component

to generate two components, specify the template asarticle, and the specified template will be automatically imported intoarticle.modules.ts, and registered to thedeclarationsarray,Note: Do not delete the components registered indeclarations, otherwise some of the components specified in the component will not be available

    ng g component Description
  • ng g component article/article-list -m=article
  • , generated in thearticlefolderarticle-listFolder component
  • ng g component article/article-create -m=article
  • , generated under thearticlefolderarticle-createFolder component
  • 4,
article.module.ts

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@NgModuledecorator just like in the root module; the second one importsCommonModule, which Many common directives likengIfandngForare provided. 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
5,

article-routing.module.ts

,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.

For example, the root route is set to
    article
  • , the setting here islist, and the access address needs to bearticle/list
  • Detailed explanation of root templates and attribute templates in Angular
    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, which can be configured 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, root module routing

app-routing.module.ts

loadChildrenuses thelazy loading feature moduledefault 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 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 root templates and attribute templates in Angularor
  • article/create

  • Detailed explanation of root templates and attribute templates in Angular
  • For more programming-related knowledge, please visit:
Programming Teaching

! !

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!

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!