angular - 学习 Angular 以及决定主屏幕 html 的因素
问题描述
在 Angular 8 中,路由 app-routing.module.ts 很简单,如果 app.component.html 有链接和文本,则可以正确使用该应用程序。
在我的研究中,我遇到了一个 app.component.html 像这样的应用程序;
<body>
<app-nav-menu></app-nav-menu>
<div class="container">
<router-outlet></router-outlet>
</div>
</body>
我在这里看到HomeComponent
应用程序启动时已加载并了解 app.module.ts 中的声明和设置,
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { RouterModule } from '@angular/router';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatCardModule } from '@angular/material/card';
import { MatListModule } from '@angular/material/list';
import { MatSelectModule } from '@angular/material/select';
import { MatInputModule } from '@angular/material/input';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { AppComponent } from './app.component';
import { NavMenuComponent } from './nav-menu/nav-menu.component';
import { HomeComponent } from './home/home.component';
import { ContactComponent } from './contact/contact.component';
import { AddContactComponent } from './add-contact/add-contact.component';
@NgModule({
declarations: [
AppComponent,
NavMenuComponent,
HomeComponent,
ContactComponent,
AddContactComponent
],
imports: [
BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
HttpClientModule,
FormsModule,
BrowserAnimationsModule,
MatCardModule,
MatListModule,
MatSelectModule,
MatInputModule,
MatButtonModule,
MatIconModule,
ReactiveFormsModule,
RouterModule.forRoot([
{ path: '', component: HomeComponent, pathMatch: 'full' },
{ path: 'contact/:id', component: ContactComponent },
{ path: 'new-contact', component: AddContactComponent }
])
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
但是HomeComponent
设置为路由器插座起始页的位置在哪里?
RouterModule.forRoot
解决方案
确切地!在引导“AppComponent”时,路由器模块评估传入的浏览器路径表达式并将控制权委托给相应的 Angular 组件。
/*https://<domain-name>:<optional-port>/ - Will take you to HomeComponent
https://<domain-name>:<optional-port>/contact - Will take you to ContactComponent with route param id:undefined or null
https://<domain-name>:<optional-port>/contact:100 - Will take you to ContactComponent with route param id:100*/
RouterModule.forRoot([
{ path: '', component: HomeComponent, pathMatch: 'full' },
...])
路由全部基于我们使用的配置,RouterModule.forRoot()
.
推荐阅读
- srcset - 您可以将 width="" 添加到 srcset 以便 Google PageSpeed 不要求明确的宽度和高度吗?
- java - 您是否知道 hibernate-enhance-maven-plugin 和 spring-boot-starter-data-elasticsearch 之间的不兼容性?
- bash - 在另一个磁盘 linux 中归档文件
- django - DRF 创建自定义关系数据或类似的东西?
- python - 在另一个窗口上嵌入使用 QtDesigner 创建的弹出窗口
- visual-studio-code - LiveServer 显示空白屏幕
- api - 空手道:如何在示例测试数据中发送整数
- android - 从 gradle 中排除约束布局依赖项
- c++ - Visual Studio 字符编码问题
- kotlin - 流的 onCompletion 和 try/finally 有什么区别?