当前位置: 首页 > news >正文

生产环境多页面(多个组件)构建一个完整的 Angular 项目框架最佳实践与性能优化

公司新项目采用了这个技术栈,从0到上线的过程中积累了不少经验,今天分享出来供大家参考。

  1. 路由导航
  2. 多页面(多个组件)
  3. 动态加载
  4. 简洁可维护的结构

第一步:项目结构示意图

angular-sendemail/
│
├── src/
│   ├── app/
│   │   ├── components/
│   │   │   ├── report/
│   │   │   │   ├── report.component.ts
│   │   │   │   ├── report.component.html
│   │   │   │   └── report.component.css
│   │   │   ├── about/
│   │   │   │   └── ...
│   │   ├── app-routing.module.ts   ✅ 路由配置文件
│   │   ├── app.component.ts        ✅ 根组件逻辑
│   │   ├── app.component.html      ✅ 包含 <router-outlet>│   │   └── app.module.ts           ✅ 应用模块└── ...

第二步:创建两个组件

ng generate component components/report
ng generate component components/about

第三步:配置路由(app-routing.module.ts)

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { ReportComponent } from './components/report/report.component';
import { AboutComponent } from './components/about/about.component';
const routes: Routes = [
{ path: 'sendemail', component: ReportComponent },
{ path: 'about', component: AboutComponent },
{ path: '', redirectTo: 'sendemail', pathMatch: 'full' }, // 默认路径
{ path: '**', redirectTo: 'sendemail' } // 未知路径重定向
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}

第四步:在 app.module.ts 中导入路由模块

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { ReportComponent } from './components/report/report.component';
import { AboutComponent } from './components/about/about.component';
import { AppRoutingModule } from './app-routing.module'; // ✅ 导入路由模块
@NgModule({
declarations: [
AppComponent,
ReportComponent,
AboutComponent
],
imports: [
BrowserModule,
AppRoutingModule // ✅ 注入
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}

第五步:配置模板 app.component.html

<!-- src/app/app.component.html --><h1> Angular 多页面项目</h1><!-- 路由导航链接 --><nav><a routerLink="/sendemail">发送邮件</a> |<a routerLink="/about">关于我们</a></nav><hr /><!-- 路由组件显示的位置 --><router-outlet></router-outlet>

第六步:组件示例 HTML(例如 report.component.html)

<div class="container"><h2>发送 Eligibility Report</h2><button (click)="sendEmail()">点击发送邮件</button></div>

第七步:组件ts(例如 report.component.ts)

import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-report',
templateUrl: './report.component.html',
})
export class ReportComponent {
constructor(private http: HttpClient) {}
sendEmail() {
const report = {
fileName: 'test_aug_05.csv',
recipientEmail: 'recipient@example.com',
healthPlanName:'test',
totalRecords: 100,
successfullyIngested: 98,
rejectedRecords: 2,
submissionDateTime:'2025-08-05 14:22',
rejectedDetails: [
{
recordNumber: 1,
memberId: 'A1B2C3',
errorReason: 'Invalid Member ID Format'
},
{
recordNumber: 2,
memberId: 'XYZ456',
errorReason: 'Missing Date of Birth (DOB)'
}
]
};
// 调用后端发送邮件 API
this.http.post('http://localhost:8080/api/reports/send', report, { responseType: 'text' })
.subscribe({
next: (res) => alert('✅ 邮件发送成功: ' + res),
error: (err) => alert('❌ 发送失败: ' + err.message)
});
}
}

效果预览:

访问地址页面显示
http://localhost:4200/自动跳转到 sendemail,显示发送邮件页面
http://localhost:4200/about显示关于页面
http://localhost:4200/unknown自动跳转回 sendemail

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

http://www.jsqmd.com/news/379543/

相关文章:

  • debian 13 安装配置ftp 创建用户admin可以访问 /mnt/Data/
  • Java计算机毕设之基于Springboot的植物健康管理病虫害防治预防系统设计与实现(完整前后端代码+说明文档+LW,调试定制等)
  • Solution - P4254 [JSOI2008] Blue Mary 开公司
  • 谈谈VR,AR
  • 热门沃尔玛购物卡回收平台精选指南 - 京顺回收
  • 检测仪供应商深度解析:产品线与技术实力探讨,测厚仪/热封仪/测量仪/试验机/分析仪/扭矩仪/测试仪,检测仪厂家推荐排行榜 - 品牌推荐师
  • 视频格式转换工具软件:HD Video Converter Factory Pro绿色版,音频转换,视频转换,图片转视频,视频下载,多视频合成等
  • 【毕业设计】基于SpringBoot的招聘求职平台的设计与实现(源码+文档+远程调试,全bao定制等)
  • 【毕业设计】基于SpringBoot技术的流浪动物管理系统的设计与实现(源码+文档+远程调试,全bao定制等)
  • 价值投资者的修炼:如何在中国市场中保持耐心
  • AI原生应用如何改变传统人机交互模式?
  • 【计算机毕业设计案例】基于Web的文物知识普及系统设计与实现(程序+文档+讲解+定制)
  • 数据湖在大数据领域的数据分析工具集成
  • 【计算机毕业设计案例】基于springboot的流浪动物救助系统(程序+文档+讲解+定制)
  • 大数据时代,列式存储在企业中的应用案例
  • 【计算机毕业设计案例】基于javaweb+springboot的高校学生社团活动管理系统基于web的社团申请和审批系统(程序+文档+讲解+定制)
  • 移动开发内存优化:从Java Heap到Native Memory
  • 【计算机毕业设计案例】基于SpringBoot的招聘求职平台基于SpringBoot招聘信息管理系统的设计与实现(程序+文档+讲解+定制)
  • Java毕设项目推荐-基于springboot的流浪动物救助系统【附源码+文档,调试定制服务】
  • 【计算机毕业设计案例】基于SpringBoot技术的流浪动物管理系统的设计与实现(程序+文档+讲解+定制)
  • Java毕设项目推荐-基于web的社团申请和审批系统基于javaweb的高校社团管理系统【附源码+文档,调试定制服务】
  • 多项式笔记
  • 实用指南:Vue3 + Element Plus 表格复选框踩坑记录
  • 如何通过集体好奇心提升市场洞察能力
  • P4015 运输问题
  • Java毕设项目推荐-springboot基于WIFI协议的大学课堂点名系统的设计与实现 基于Spring Boot的智能点名管理系统【附源码+文档,调试定制服务】
  • Java毕设项目推荐-基于SpringBoot+Vue的求职招聘平台设计与实现基于SpringBoot的招聘求职平台的设计与实现【附源码+文档,调试定制服务】
  • 2026-02-13学习
  • 春节期间杂题练习
  • 装修 绿植 中古风