面试题答案
一键面试1. 懒加载服务
使用Angular的懒加载模块特性
- 设计模式:模块懒加载模式。
- 具体实现步骤:
- 创建独立模块:将需要懒加载的服务及其相关逻辑封装在一个独立的Angular模块中。例如,创建
ExpensiveServiceModule
,并在其中声明ExpensiveService
。
@NgModule({ providers: [ExpensiveService], declarations: [], imports: [] }) export class ExpensiveServiceModule {}
- 配置路由进行懒加载:在主路由模块中,配置路由来实现模块的懒加载。
这样,只有当访问到const routes: Routes = [ { path: 'feature', loadChildren: () => import('./expensive - service.module').then(m => m.ExpensiveServiceModule) } ];
feature
路由时,ExpensiveServiceModule
及其包含的ExpensiveService
才会被加载。 - 创建独立模块:将需要懒加载的服务及其相关逻辑封装在一个独立的Angular模块中。例如,创建
使用 inject
函数的延迟注入
- 设计模式:延迟初始化模式。
- 具体实现步骤:
- 创建延迟注入函数:在TypeScript中,可以创建一个函数来延迟服务的注入。
import { Injector } from '@angular/core'; let expensiveService: ExpensiveService; export function lazyLoadExpensiveService(injector: Injector): ExpensiveService { if (!expensiveService) { expensiveService = injector.get(ExpensiveService); } return expensiveService; }
- 在组件中使用:在组件中,通过
Injector
实例调用该函数来实现延迟注入。
import { Component, Injector } from '@angular/core'; @Component({ selector: 'app - my - component', templateUrl: './my - component.html' }) export class MyComponent { constructor(private injector: Injector) {} ngOnInit() { const service = lazyLoadExpensiveService(this.injector); // 使用 service } }
2. 优化依赖管理
使用依赖注入容器
- 设计模式:依赖注入模式。
- 具体实现步骤:
- 在模块中提供服务:在Angular模块的
providers
数组中提供服务。
@NgModule({ providers: [ExpensiveService], declarations: [], imports: [] }) export class AppModule {}
- 在组件中注入服务:在组件的构造函数中声明依赖,Angular的依赖注入容器会自动提供相应的服务实例。
@Component({ selector: 'app - my - component', templateUrl: './my - component.html' }) export class MyComponent { constructor(private expensiveService: ExpensiveService) {} }
- 在模块中提供服务:在Angular模块的
使用 @Injectable({ providedIn: 'root' })
- 设计模式:全局单例模式。
- 具体实现步骤:
- 标记服务为全局提供:在服务类上使用
@Injectable({ providedIn: 'root' })
装饰器。
这样,Angular会自动在根模块中提供该服务,保证整个应用中只有一个服务实例,避免重复实例化带来的开销。@Injectable({ providedIn: 'root' }) export class ExpensiveService {}
- 标记服务为全局提供:在服务类上使用
使用 ɵInjectFlags.SkipSelf
来优化依赖查找
- 设计模式:优化依赖查找模式。
- 具体实现步骤:
- 在组件或指令中使用:当注入服务时,可以指定
ɵInjectFlags.SkipSelf
标志。
这会使依赖注入系统跳过当前组件及其祖先组件的注入器,直接在更上层的注入器中查找服务,有助于避免不必要的服务实例化和解决依赖循环问题。import { Component, Inject, ɵInjectFlags } from '@angular/core'; @Component({ selector: 'app - my - component', templateUrl: './my - component.html' }) export class MyComponent { constructor( @Inject(ExpensiveService, { host: true, skipSelf: true, optional: true }) private expensiveService: ExpensiveService ) {} }
- 在组件或指令中使用:当注入服务时,可以指定