Angular(通常指 Angular 2+)是由 Google 开发和维护的 TypeScript 前端框架,于 2016 年发布,是 AngularJS 的完全重写版本。Angular 是一个 完整的 MVC 框架,提供开箱即用的完整解决方案。
Angular 的核心定位是 企业级 TypeScript 应用框架。它提供了:
AngularJS 由 Misko Hevery 于 2010 年创建,是第一个将 MVC 模式引入前端的框架。2016 年,Angular 团队完全重写了框架,使用 TypeScript 构建,发布了 Angular 2.0。
// app.component.ts
import { Component } from "@angular/core";
@Component({
selector: "app-root",
template: `
{{ title }}
你好, {{ name }}!
5">计数超过 5
`,
styles: [`
h1 { color: blue; }
`]
})
export class AppComponent {
title = "Angular 应用";
count = 0;
name = "";
increment() {
this.count++;
}
}
// app.module.ts
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { AppComponent } from "./app.component";
import { UserComponent } from "./user/user.component";
@NgModule({
declarations: [
AppComponent,
UserComponent
],
imports: [
BrowserModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
// user.service.ts
import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { Observable } from "rxjs";
@Injectable({
providedIn: "root"
})
export class UserService {
private apiUrl = "https://api.example.com/users";
constructor(private http: HttpClient) { }
getUsers(): Observable {
return this.http.get(this.apiUrl);
}
getUser(id: number): Observable {
return this.http.get(`${this.apiUrl}/${id}`);
}
createUser(user: User): Observable {
return this.http.post(this.apiUrl, user);
}
}
// 在组件中使用
@Component({...})
export class UserListComponent {
constructor(private userService: UserService) { }
users: User[] = [];
ngOnInit() {
this.userService.getUsers().subscribe(users => {
this.users = users;
});
}
}
// app-routing.module.ts
import { NgModule } from "@angular/core";
import { RouterModule, Routes } from "@angular/router";
import { HomeComponent } from "./home/home.component";
import { AboutComponent } from "./about/about.component";
import { UserDetailComponent } from "./user-detail/user-detail.component";
const routes: Routes = [
{ path: "", component: HomeComponent },
{ path: "about", component: AboutComponent },
{ path: "users/:id", component: UserDetailComponent },
{ path: "**", redirectTo: "" }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
// 使用路由
// app.component.html
// 组件
import { Component } from "@angular/core";
import { FormBuilder, FormGroup, Validators } from "@angular/forms";
@Component({
template: `
`
})
export class UserFormComponent {
userForm: FormGroup;
constructor(private fb: FormBuilder) {
this.userForm = this.fb.group({
name: ["", [Validators.required, Validators.minLength(2)]],
email: ["", [Validators.required, Validators.email]]
});
}
onSubmit() {
if (this.userForm.valid) {
console.log(this.userForm.value);
}
}
}
// 使用 RxJS
import { Component } from "@angular/core";
import { Observable, of } from "rxjs";
import { map, filter, debounceTime, switchMap } from "rxjs/operators";
@Component({...})
export class SearchComponent {
searchTerm = new Subject();
results: Observable = this.searchTerm.pipe(
debounceTime(300),
distinctUntilChanged(),
switchMap(term => this.search(term))
);
search(term: string): Observable {
return this.http.get(`/api/search?q=${term}`);
}
}
| 对比项 | Angular | React | Vue |
|---|---|---|---|
| 类型 | 全栈框架 | UI 库 | 渐进式框架 |
| 语言 | TypeScript | JS/TS | JS/TS |
| 学习曲线 | 陡峭 | 中等 | 平缓 |
| 大厂背书 | Meta | 独立 | |
| 企业级 | ✅ 原生 | ✅ 需组合 | ✅ 需组合 |
| 适用场景 | 大型企业 | 通用 | 中小型 |
TypeScript 基础、RxJS 基础
Angular CLI、组件、模板语法、模块
依赖注入、服务、路由、表单、HTTP 客户端
RxJS 高级、状态管理(NgRx)、SSR、性能优化
Angular 是企业级前端开发的重型武器。
它用 TypeScript、依赖注入、RxJS 提供了完整的企业级解决方案。Angular 是大型团队和企业项目的理想选择。
"Angular 让企业级前端开发变得规范而高效。" 🔷