全面分析一下前端框架Angular的来龙去脉,分析angular的技术要点和难点,以及详细的语法和使用规则,底层原理-小白进阶之路

本文主要是介绍全面分析一下前端框架Angular的来龙去脉,分析angular的技术要点和难点,以及详细的语法和使用规则,底层原理-小白进阶之路,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Angular 前端框架全面分析

Angular 是一个由 Google 维护的开源前端框架。它最早在 2010 年发布,最初版本称为 AngularJS。2016 年,团队发布了一个完全重写的版本,称为 Angular 2,之后的版本(如 Angular 4、Angular 5 等)都统称为 Angular。

一、历史背景

1. AngularJS(Angular 1.x)

  • 发布年份:2010
  • 特点:基于 MVC(Model-View-Controller)架构,使用双向数据绑定和依赖注入。
  • 限制:性能瓶颈、可维护性差、学习曲线陡峭。

2. Angular 2+

  • 发布年份:2016
  • 特点:完全重写,采用基于组件的架构,使用 TypeScript 开发,提升了性能和可维护性。
  • 发展:从 Angular 2 开始,Angular 团队每半年发布一个大版本,当前最新版本为 Angular 15。

二、技术要点

1. TypeScript

Angular 使用 TypeScript 编写,提供了静态类型检查、现代 JavaScript 特性等优点,有助于提高代码质量和开发效率。

2. 组件化

采用组件式开发,将应用程序分割成独立的、可复用的组件,每个组件包含自己的模板、样式和逻辑。

3. 模块化

使用 NgModule 来组织代码,一个 NgModule 可以包含组件、指令、管道和服务等,提供了良好的模块化支持。

4. 依赖注入

Angular 提供了强大的依赖注入机制,简化了服务的创建和管理,提高了代码的可测试性和可维护性。

5. 模板语法

Angular 使用直观的模板语法来定义组件的视图,支持数据绑定、指令和事件绑定等。

6. 路由

Angular 内置强大的路由模块,可以轻松地定义应用程序的导航和页面切换。

7. 表单处理

提供了两种表单处理方式:模板驱动表单和响应式表单,满足不同需求。

8. RxJS

Angular 大量使用 RxJS(Reactive Extensions for JavaScript)来处理异步数据流,非常适合复杂的数据交互场景。

三、技术难点

1. 学习曲线

由于 Angular 涉及的概念较多(如依赖注入、RxJS、TypeScript 等),学习曲线相对较陡。

2. 性能优化

理解和应用 Angular 的变更检测机制和 Zone.js 是性能优化的关键,需要一定的深入学习。

3. 复杂项目管理

随着项目规模的扩大,如何有效地管理模块、组件、服务等也是一个挑战。

四、详细语法和使用规则

1. 组件

import { Component } from '@angular/core';@Component({selector: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.css']
})
export class AppComponent {title = 'my-angular-app';
}

2. 模块

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';import { AppComponent } from './app.component';@NgModule({declarations: [AppComponent],imports: [BrowserModule],providers: [],bootstrap: [AppComponent]
})
export class AppModule { }

3. 数据绑定

  • 插值绑定{{ expression }}
  • 属性绑定[property]="expression"
  • 事件绑定(event)="handler"
  • 双向绑定[(ngModel)]="property"

4. 指令

  • 结构型指令*ngIf, *ngFor
  • 属性型指令[ngClass], [ngStyle]

5. 服务和依赖注入

import { Injectable } from '@angular/core';@Injectable({providedIn: 'root',
})
export class DataService {getData() {return ['data1', 'data2', 'data3'];}
}

6. 路由

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { AboutComponent }```typescript
import { AboutComponent } from './about/about.component';const routes: Routes = [{ path: '', component: HomeComponent },{ path: 'about', component: AboutComponent }
];@NgModule({imports: [RouterModule.forRoot(routes)],exports: [RouterModule]
})
export class AppRoutingModule { }

7. 模板驱动表单

<form #form="ngForm" (ngSubmit)="onSubmit(form)"><input type="text" name="username" ngModel required><button type="submit">Submit</button>
</form>

8. 响应式表单

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';@Component({selector: 'app-form',templateUrl: './form.component.html'
})
export class FormComponent implements OnInit {form: FormGroup;constructor(private fb: FormBuilder) { }ngOnInit() {this.form = this.fb.group({username: ['', Validators.required]});}onSubmit() {console.log(this.form.value);}
}
<form [formGroup]="form" (ngSubmit)="onSubmit()"><input formControlName="username"><button type="submit">Submit</button>
</form>

五、底层原理

1. 变更检测机制

Angular 使用 Zone.js 来捕获异步操作,然后触发变更检测机制来更新视图。变更检测机制的核心是脏值检测策略,Angular 会对比数据的当前值和之前的值,决定是否重新渲染视图。

2. 依赖注入机制

Angular 的依赖注入机制基于提供者(Provider)和注入器(Injector)。每个服务、组件等都可以声明依赖项,Angular 会根据依赖关系图自动注入所需的依赖。

3. 编译过程

Angular 的编译过程分为 AOT(Ahead-of-Time)和 JIT(Just-in-Time)两种模式:

  • AOT 编译:在构建时进行编译,生成优化后的 JavaScript 代码,减少应用运行时的开销,提高性能。
  • JIT 编译:在浏览器中运行时进行编译,适合开发阶段使用。

4. 渲染机制

Angular 使用虚拟 DOM 进行渲染优化,类似于 React。通过虚拟 DOM,Angular 可以高效地计算出需要更新的部分,减少直接操作真实 DOM 的次数,从而提高性能。

5. 模块化

Angular 的模块系统通过 NgModule 实现。每个 NgModule 都有自己的执行环境,可以包含组件、指令、管道和服务等。模块化设计有助于代码分离和按需加载,提高了应用的可维护性和性能。

六、模块化

Angular 是一个功能强大且全面的前端框架,适用于大规模、复杂的 Web 应用开发。它采用 TypeScript 进行开发,提供了丰富的工具和功能(如依赖注入、变更检测、模块化、路由等),尽管学习曲线较陡,但在大型项目中表现出色。掌握 Angular 的核心概念和原理,能够帮助开发者构建高性能、高可维护性的 Web 应用。
当然,下面是关于 Angular 的更多详细信息,包括一些高级特性、最佳实践以及常见的开发模式。

七、高级特性

1. 懒加载(Lazy Loading)

懒加载是 Angular 中提高性能的重要特性。通过懒加载,可以按需加载模块而不是在应用启动时加载所有模块。

const routes: Routes = [{ path: 'home', component: HomeComponent },{ path: 'about', loadChildren: () => import('./about/about.module').then(m => m.AboutModule) }
];

2. Angular CLI

Angular CLI 是一个强大的命令行工具,用于初始化、开发、构建和维护 Angular 应用程序。

# 安装 Angular CLI
npm install -g @angular/cli# 创建新项目
ng new my-angular-app# 启动开发服务器
ng serve# 生成组件、服务等
ng generate component my-component

3. 国际化(i18n)

Angular 提供了内置的国际化支持,可以轻松地将应用程序本地化到多种语言。

import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';registerLocaleData(localeFr, 'fr');

4. 动态组件加载

在运行时动态加载和渲染组件是 Angular 的一个高级特性,适用于需要根据用户交互动态生成内容的场景。

import { Component, ComponentFactoryResolver, ViewChild, ViewContainerRef } from '@angular/core';@Component({selector: 'app-dynamic-loader',template: `<ng-container #container></ng-container>`
})
export class DynamicLoaderComponent {@ViewChild('container', { read: ViewContainerRef, static: true }) container: ViewContainerRef;constructor(private resolver: ComponentFactoryResolver) { }loadComponent(component: any) {const factory = this.resolver.resolveComponentFactory(component);this.container.clear();this.container.createComponent(factory);}
}

5. 服务端渲染(SSR)

使用 Angular Universal 可以实现服务端渲染,从而提高应用的 SEO 和首屏加载速度。

# 安装 Angular Universal
ng add @nguniversal/express-engine# 构建和运行 SSR 应用
npm run build:ssr
npm run serve:ssr

八、最佳实践

1. 代码组织

  • 模块化设计:将应用分为多个功能模块,每个模块负责特定功能。
  • 组件化设计:尽量将界面划分为独立的组件,提升可复用性。
  • 服务层:将业务逻辑和数据访问层抽象为服务,组件只负责视图逻辑。

2. 性能优化

  • 使用懒加载:按需加载模块,减少首屏加载时间。
  • 避免不必要的变更检测:使用 OnPush 变更检测策略,减少不必要的视图更新。
  • 异步操作:尽量使用异步操作,如 async 管道、PromiseObservable

3. 代码规范

  • 使用 TypeScript:充分利用 TypeScript 的静态类型检查,提升代码质量。
  • 遵循 Angular 风格指南:遵循官方提供的风格指南,保持代码一致性和可维护性。
  • 单元测试和端到端测试:编写单元测试和端到端测试,确保代码的正确性和稳定性。

九、常见开发模式

1. 智能组件和哑组件

  • 智能组件:负责处理业务逻辑和与服务的交互。
  • 哑组件:只负责展示数据和处理简单的用户交互。
// 智能组件
@Component({selector: 'app-smart',template: `<app-dumb [data]="data" (event)="handleEvent($event)"></app-dumb>`
})
export class SmartComponent {data = this.dataService.getData();constructor(private dataService: DataService) { }handleEvent(event: any) { /* 处理事件 */ }
}// 哑组件
@Component({selector: 'app-dumb',template: `<div *ngFor="let item of data">{{ item }}</div>`
})
export class DumbComponent {@Input() data: any[];@Output() event = new EventEmitter<any>();
}

2. 状态管理

对于复杂的应用,可以采用状态管理库(如 NgRx)来管理应用状态当然,接下来我会详细介绍 Angular 的状态管理、单元测试、与其他技术的集成,以及一些高级调试技巧。

十、状态管理

1. NgRx

NgRx 是 Angular 中常用的状态管理库,基于 Redux 架构,提供了单一状态树、不可变状态、纯函数 reducer 等特点。

安装 NgRx
ng add @ngrx/store
ng add @ngrx/effects
状态定义

定义状态接口和初始状态:

// state.ts
export interface AppState {count: number;
}export const initialState: AppState = {count: 0,
};
Actions

定义 actions:

// actions.ts
import { createAction, props } from '@ngrx/store';export const increment = createAction('[Counter] Increment');
export const decrement = createAction('[Counter] Decrement');
export const reset = createAction('[Counter] Reset', props<{ count: number }>());
Reducer

定义 reducer:

// reducer.ts
import { createReducer, on } from '@ngrx/store';
import { increment, decrement, reset } from './actions';
import { AppState, initialState } from './state';const _counterReducer = createReducer(initialState,on(increment, state => ({ ...state, count: state.count + 1 })),on(decrement, state => ({ ...state, count: state.count - 1 })),on(reset, (state, { count }) => ({ ...state, count }))
);export function counterReducer(state: AppState | undefined, action: Action) {return _counterReducer(state, action);
}
Store Module

在应用模块中引入 Store 模块:

import { StoreModule } from '@ngrx/store';
import { counterReducer } from './reducer';@NgModule({imports: [StoreModule.forRoot({ count: counterReducer }),],
})
export class AppModule { }
使用 Store

在组件中使用 Store:

import { Component } from '@angular/core';
import { Store } from '@ngrx/store';
import { AppState } from './state';
import { increment, decrement, reset } from './actions';@Component({selector: 'app-counter',template: `<div>Count: {{ count$ | async }}</div><button (click)="increment()">Increment</button><button (click)="decrement()">Decrement</button><button (click)="reset(0)">Reset</button>`
})
export class CounterComponent {count$ = this.store.select(state => state.count);constructor(private store: Store<AppState>) {}increment() {this.store.dispatch(increment());}decrement() {this.store.dispatch(decrement());}reset(value: number) {this.store.dispatch(reset({ count: value }));}
}

2. Akita

Akita 是另一种用于 Angular 的状态管理库,提供了灵活的状态管理方式,适合大型应用。

安装 Akita
ng add @datorama/akita
定义 Store
import { Store, StoreConfig } from '@datorama/akita';export interface AppState {count: number;
}export function createInitialState(): AppState {return {count: 0,};
}@StoreConfig({ name: 'app' })
export class AppStore extends Store<AppState> {constructor() {super(createInitialState());}
}
使用 Store
import { Component } from '@angular/core';
import { AppStore } from './app.store';@Component({selector: 'app-counter',template: `<div>Count: {{ count$ | async }}</div><button (click)="increment()">Increment</button><button (click)="decrement()">Decrement</button><button (click)="reset()">Reset</button>`
})
export class CounterComponent {count$ = this.appStore.select(state => state.count);constructor(private appStore: AppStore) {}increment() {this.appStore.update(state => ({ count: state.count + 1 }));}decrement() {this.appStore.update(state => ({ count: state.count - 1 }));}reset() {this.appStore.reset();}
}

十一、单元测试

Angular 提供了强大的单元测试支持,使用 Karma 和 Jasmine 进行测试。

1. 设置测试环境

Angular CLI 自动生成的项目已经配置好了 Karma 和 Jasmine,无需额外配置。

2. 编写测试当然,我将继续深入探讨 Angular 的单元测试、与其他技术的集成、以及一些高级调试技巧。

十一、单元测试(继续)

2. 编写测试

组件测试

使用 TestBed 创建组件并进行测试:

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { CounterComponent } from './counter.component';describe('CounterComponent', () => {let component: CounterComponent;let fixture: ComponentFixture<CounterComponent>;beforeEach(async () => {await TestBed.configureTestingModule({declarations: [ CounterComponent ]}).compileComponents();});beforeEach(() => {fixture = TestBed.createComponent(CounterComponent);component = fixture.componentInstance;fixture.detectChanges();});it('should create', () => {expect(component).toBeTruthy();});it('should increment count', () => {component.increment();expect(component.count).toBe(1);});it('should decrement count', () => {component.decrement();expect(component.count).toBe(-1);});it('should reset count', () => {component.reset(0);expect(component.count).toBe(0);});
});
服务测试

测试服务时,使用 TestBed 配置提供者:

import { TestBed } from '@angular/core/testing';
import { DataService } from './data.service';describe('DataService', () => {let service: DataService;beforeEach(() => {TestBed.configureTestingModule({});service = TestBed.inject(DataService);});it('should be created', () => {expect(service).toBeTruthy();});it('should get data', () => {const data = service.getData();expect(data).toEqual(['item1', 'item2']);});
});

3. 运行测试

使用 Angular CLI 运行测试:

ng test

十二、与其他技术的集成

1. 与后端 API 的集成

使用 Angular 的 HttpClient 模块来与后端 API 进行通信。

安装 HttpClient 模块
import { HttpClientModule } from '@angular/common/http';@NgModule({imports: [ HttpClientModule ],
})
export class AppModule { }
创建服务
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';@Injectable({providedIn: 'root',
})
export class ApiService {constructor(private http: HttpClient) {}getData(): Observable<any> {return this.http.get('https://api.example.com/data');}postData(data: any): Observable<any> {return this.http.post('https://api.example.com/data', data);}
}
使用服务
import { Component, OnInit } from '@angular/core';
import { ApiService } from './api.service';@Component({selector: 'app-data',template: `<div *ngFor="let item of data">{{ item }}</div>`,
})
export class DataComponent implements OnInit {data: any;constructor(private apiService: ApiService) {}ngOnInit() {this.apiService.getData().subscribe(data => this.data = data);}
}

2. 与第三方库的集成

Angular 可以轻松集成第三方库,如 RxJS、Lodash 等。

使用 RxJS
import { of } from 'rxjs';
import { map } from 'rxjs/operators';const observable = of(1, 2, 3).pipe(map(x => x * 2)
);observable.subscribe(value => console.log(value)); // 输出 2, 4, 6
使用 Lodash
import * as _ from 'lodash';const array = [1, 2, 3, 4];
const doubled = _.map(array, x => x * 2);
console.log(doubled); // 输出 [2, 4, 6, 8]

当然,我将继续介绍更多关于 Angular 的内容,包括高级调试技巧、动画、表单处理、以及一些常见的开发工具和资源。

十三、高级调试技巧(继续)

2. 使用 Debug 模式

启用 Debug 模式

在开发过程中,可以启用 Angular 的 Debug 模式,以便在控制台中查看详细的调试信息。

import { enableProdMode } from '@angular/core';if (environment.production) {enableProdMode();
} else {// 开发模式下启用调试信息import('zone.js/plugins/zone-error');  // Included with Angular CLI.
}

3. Angular 调试工具

使用 ng.probe

在浏览器控制台中,可以使用 ng.probe 方法来访问组件实例和调试信息。

// 获取组件实例
const component = ng.probe(document.querySelector('app-root')).componentInstance;
console.log(component);
使用 Augury

Augury 是一个用于调试 Angular 应用的 Chrome 扩展,提供组件树、依赖注入、路由等信息的可视化展示。

安装 Augury
  • 在 Chrome 浏览器中,访问 Augury 页面并安装扩展。
使用 Augury
  • 打开 Chrome 开发者工具,切换到 Augury 标签,可以查看组件树、依赖关系等信息。

十四、动画

Angular 提供了强大的动画支持,通过 Angular Animations 模块可以实现复杂的动画效果。

1. 安装 Angular Animations

import { BrowserAnimationsModule } from '@angular/platform-browser/animations';@NgModule({imports: [BrowserAnimationsModule],
})
export class AppModule {}

2. 定义动画

使用 triggerstatestyletransitionanimate 来定义动画。

import { trigger, state, style, transition, animate } from '@angular/animations';@Component({selector: 'app-animated',template: `<div [@fadeInOut]="'in'">Fade In Out Animation</div><button (click)="toggleState()">Toggle State</button>`,animations: [trigger('fadeInOut', [state('in', style({ opacity: 1 })),transition(':enter', [style({ opacity: 0 }),animate(600)]),transition(':leave', [animate(600, style({ opacity: 0 }))])])]
})
export class AnimatedComponent {state = 'in';toggleState() {this.state = this.state === 'in' ? 'out' : 'in';}
}

十五、表单处理

Angular 提供了两种表单处理方式:模板驱动表单和响应式表单。

1. 模板驱动表单

模板驱动表单使用 Angular 的指令和模板语法来处理表单。

import { FormsModule } from '@angular/forms';@NgModule({imports: [FormsModule],
})
export class AppModule {}
<!-- template-driven-form.component.html -->
<form #form="ngForm" (ngSubmit)="onSubmit(form)"><label for="name">Name:</label><input type="text" id="name" name="name" ngModel><button type="submit">Submit</button>
</form>
// template-driven-form.component.ts
import { Component } from '@angular/core';@Component({selector: 'app-template-driven-form',templateUrl: './template-driven-form.component.html'
})
export class TemplateDrivenFormComponent {onSubmit(form: any) {console.log('Form Data:', form.value);}
}

2. 响应式表单

响应式表单使用 FormBuilderFormGroup 来处理表单。

import { ReactiveFormsModule, FormBuilder, FormGroup } from '@angular/forms';@NgModule({imports: [ReactiveFormsModule],
})
export class AppModule {}
<!-- reactive-form.component.html -->
<form [formGroup]="form" (ngSubmit)="onSubmit()"><label for="name">Name:</label><input type="text" id="name" formControlName="name"><button type="submit">Submit</button>
</form>
// reactive-form.component.ts
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';@Component({selector: 'app-reactive-form',templateUrl: './react### 2. **响应式表单**响应式表单使用 `FormBuilder``FormGroup` 来处理表单。#### 创建表单```typescript
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';@Component({selector: 'app-reactive-form',templateUrl: './reactive-form.component.html'
})
export class ReactiveFormComponent implements OnInit {form: FormGroup;constructor(private fb: FormBuilder) {}ngOnInit() {this.form = this.fb.group({name: ['', Validators.required],email: ['', [Validators.required, Validators.email]],password: ['', [Validators.required, Validators.minLength(6)]]});}onSubmit() {if (this.form.valid) {console.log('Form Data:', this.form.value);} else {console.log('Form is invalid');}}
}
表单模板
<form [formGroup]="form" (ngSubmit)="onSubmit()"><div><label for="name">Name:</label><input type="text" id="name" formControlName="name"><div *ngIf="form.get('name').invalid && form.get('name').touched">Name is required</div></div><div><label for="email">Email:</label><input type="email" id="email" formControlName="email"><div *ngIf="form.get('email').invalid && form.get('email').touched">Enter a valid email</div></div><div><label for="password">Password:</label><input type="password" id="password" formControlName="password"><div *ngIf="form.get('password').invalid && form.get('password').touched">Password must be at least 6 characters long</div></div><button type="submit">Submit</button>
</form>

十六、路由

Angular 的路由模块允许你在应用中定义导航路径。

1. 设置路由

配置路由
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';const routes: Routes = [{ path: '', component: HomeComponent },{ path: 'about', component: AboutComponent }
];@NgModule({imports: [RouterModule.forRoot(routes)],exports: [RouterModule]
})
export class AppRoutingModule { }
导航链接
<nav><a routerLink="/">Home</a><a routerLink="/about">About</a>
</nav>
<router-outlet></router-outlet>

2. 路由守卫

路由守卫用于保护路由,确保用户具有访问权限。

import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { AuthService } from './auth.service';@Injectable({providedIn: 'root'
})
export class AuthGuard implements CanActivate {constructor(private authService: AuthService, private router: Router) {}canActivate(next: ActivatedRouteSnapshot,state: RouterStateSnapshot): boolean {if (this.authService.isLoggedIn()) {return true;} else {this.router.navigate(['/login']);return false;}}
}
使用路由守卫
const routes: Routes = [{ path: '', component: HomeComponent },{ path: 'about', component: AboutComponent, canActivate: [AuthGuard] }
];

十七、国际化

Angular 提供了内置的国际化支持,用于多语言应用程序。

1. 准备翻译文件

创建翻译文件 messages.xlf

<?xml version="1.0" encoding="UTF-8" ?>
<xliff version="1.2"><file source-language="en" datatype="plaintext" original="ng2.template"><body><trans-unit id="homeTitle" datatype="html"><source>Home</source><target>Accueil</target></当然,我将继续介绍更多关于 Angular 的内容,包括国际化的详细步骤、服务端渲染、以及一些常用的开发工具和资源。## 十七、国际化(继续)### 1. **准备翻译文件**创建翻译文件 `messages.xlf`:```xml
<?xml version="1.0" encoding="UTF-8" ?>
<xliff version="1.2"><file source-language="en" datatype="plaintext" original="ng2.template"><body><trans-unit id="homeTitle" datatype="html"><source>Home</source><target>Accueil</target></trans-unit><trans-unit id="aboutTitle" datatype="html"><source>About</source><target>À propos</target></trans-unit></body></file>
</xliff>

2. 配置 Angular

angular.json 中添加国际化配置:

{"$schema": "./node_modules/@angular/cli/lib/config/schema.json","projects": {"your-project-name": {"i18n": {"sourceLocale": "en","locales": {"fr": "src/locale/messages.fr.xlf"}},...}}
}

3. 标记可翻译文本

使用 Angular 内置的 i18n 属性标记可翻译的文本:

<h1 i18n="homeTitle">Home</h1>
<p i18n="aboutTitle">About</p>

4. 构建多语言版本

使用 Angular CLI 构建多语言版本:

ng build --localize

十八、服务端渲染(SSR)

Angular Universal 是一个用于实现 Angular 应用服务端渲染的库。

1. 安装 Angular Universal

ng add @nguniversal/express-engine

2. 更新应用模块

更新 app.server.module.ts 以适配服务端渲染:

import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import { AppModule } from './app.module';
import { AppComponent } from './app.component';@NgModule({imports: [AppModule,ServerModule],bootstrap: [AppComponent]
})
export class AppServerModule {}

3. 更新服务器文件

配置 server.ts 文件以启动 Express 服务器:

import 'zone.js/node';
import { ngExpressEngine } from '@nguniversal/express-engine';
import * as express from 'express';
import { join } from 'path';
import { APP_BASE_HREF } from '@angular/common';
import { existsSync } from 'fs';import { AppServerModule } from './src/main.server';// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {const server = express();const distFolder = join(process.cwd(), 'dist/your-project-name/browser');const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';server.engine('html', ngExpressEngine({bootstrap: AppServerModule,}));server.set('view engine', 'html');server.set('views', distFolder);// Serve static files from /browserserver.get('*.*', express.static(distFolder, {maxAge: '1y'}));// All regular routes use the Universal engineserver.get('*', (req, res) => {res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });});return server;
}function run(): void {const port = process.env.PORT || 4000;// Start up the Node serverconst server = app();server.listen(port, () => {console.log(`Node Express server listening on http://localhost:${port}`);});
}// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
declare const __non_webpack_require__: NodeRequire;const mainModule = __non_webpack_require__.main;
const moduleFilename = mainModule && mainModule.filename || '';
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {run();
}export * from './src/main.server';

4. 运行 SSR

使用 Angular CLI 构建和运行 SSR 应用:

npm run build:ssr
npm run serve:ssr

这篇关于全面分析一下前端框架Angular的来龙去脉,分析angular的技术要点和难点,以及详细的语法和使用规则,底层原理-小白进阶之路的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1088010

相关文章

C语言中联合体union的使用

本文编辑整理自: http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=179471 一、前言 “联合体”(union)与“结构体”(struct)有一些相似之处。但两者有本质上的不同。在结构体中,各成员有各自的内存空间, 一个结构变量的总长度是各成员长度之和。而在“联合”中,各成员共享一段内存空间, 一个联合变量

如何突破底层思维方式的牢笼

我始终认为,牛人和普通人的根本区别在于思维方式的不同,而非知识多少、阅历多少。 在这个世界上总有一帮神一样的人物存在。就像读到的那句话:“人类就像是一条历史长河中的鱼,只有某几条鱼跳出河面,看到世界的法则,但是却无法改变,当那几条鱼中有跳上岸,进化了,改变河道流向,那样才能改变法则。”  最近一段时间一直在不断寻在内心的东西,同时也在不断的去反省和否定自己的一些思维模式,尝试重

揭秘未来艺术:AI绘画工具全面介绍

📑前言 随着科技的飞速发展,人工智能(AI)已经逐渐渗透到我们生活的方方面面。在艺术创作领域,AI技术同样展现出了其独特的魅力。今天,我们就来一起探索这个神秘而引人入胜的领域,深入了解AI绘画工具的奥秘及其为艺术创作带来的革命性变革。 一、AI绘画工具的崛起 1.1 颠覆传统绘画模式 在过去,绘画是艺术家们通过手中的画笔,蘸取颜料,在画布上自由挥洒的创造性过程。然而,随着AI绘画工

墨刀原型工具-小白入门篇

墨刀原型工具-小白入门篇 简介 随着互联网的发展和用户体验的重要性越来越受到重视,原型设计逐渐成为了产品设计中的重要环节。墨刀作为一款原型设计工具,以其简洁、易用的特点,受到了很多设计师的喜爱。本文将介绍墨刀原型工具的基本使用方法,以帮助小白快速上手。 第一章:认识墨刀原型工具 1.1 什么是墨刀原型工具 墨刀是一款基于Web的原型设计工具,可以帮助设计师快速创建交互原型,并且可以与团队

乐鑫 Matter 技术体验日|快速落地 Matter 产品,引领智能家居生态新发展

随着 Matter 协议的推广和普及,智能家居行业正迎来新的发展机遇,众多厂商纷纷投身于 Matter 产品的研发与验证。然而,开发者普遍面临技术门槛高、认证流程繁琐、生产管理复杂等诸多挑战。  乐鑫信息科技 (688018.SH) 凭借深厚的研发实力与行业洞察力,推出了全面的 Matter 解决方案,包含基于乐鑫 SoC 的 Matter 硬件平台、基于开源 ESP-Matter SDK 的一

Tolua使用笔记(上)

目录   1.准备工作 2.运行例子 01.HelloWorld:在C#中,创建和销毁Lua虚拟机 和 简单调用。 02.ScriptsFromFile:在C#中,对一个lua文件的执行调用 03.CallLuaFunction:在C#中,对lua函数的操作 04.AccessingLuaVariables:在C#中,对lua变量的操作 05.LuaCoroutine:在Lua中,

RedHat运维-Linux文本操作基础-AWK进阶

你不用整理,跟着敲一遍,有个印象,然后把它保存到本地,以后要用再去看,如果有了新东西,你自个再添加。这是我参考牛客上的shell编程专项题,只不过换成了问答的方式而已。不用背,就算是我自己亲自敲,我现在好多也记不住。 1. 输出nowcoder.txt文件第5行的内容 2. 输出nowcoder.txt文件第6行的内容 3. 输出nowcoder.txt文件第7行的内容 4. 输出nowcode

【Linux进阶】UNIX体系结构分解——操作系统,内核,shell

1.什么是操作系统? 从严格意义上说,可将操作系统定义为一种软件,它控制计算机硬件资源,提供程序运行环境。我们通常将这种软件称为内核(kerel),因为它相对较小,而且位于环境的核心。  从广义上说,操作系统包括了内核和一些其他软件,这些软件使得计算机能够发挥作用,并使计算机具有自己的特生。这里所说的其他软件包括系统实用程序(system utility)、应用程序、shell以及公用函数库等

一份LLM资源清单围观技术大佬的日常;手把手教你在美国搭建「百万卡」AI数据中心;为啥大模型做不好简单的数学计算? | ShowMeAI日报

👀日报&周刊合集 | 🎡ShowMeAI官网 | 🧡 点赞关注评论拜托啦! 1. 为啥大模型做不好简单的数学计算?从大模型高考数学成绩不及格说起 司南评测体系 OpenCompass 选取 7 个大模型 (6 个开源模型+ GPT-4o),组织参与了 2024 年高考「新课标I卷」的语文、数学、英语考试,然后由经验丰富的判卷老师评判得分。 结果如上图所

UE3脚本UnrealScript UC语法点滴

持续更新 目录 类定义修饰符  1.dependson(CLASSNAME) 2.config(ININAME) 3.native 4.notplaceable 5.inherits(CLASSNAME1[,CLASSNAME2,...]) 类对象实例创建 类默认属性设置 变量 1.声明 var local 2.修饰符 config  3.array 类型变量 以及