概论 · Angular
零部件分类
综合模块
- 普通模块 返回
@NgModule({
declarations: [], // 组件、指令、管道 @他们共享此模块上下文
providers: [], // 服务 @并在组件中注入使用
//
imports: [], // 导入其他模块,全局仅@
exports: [], // 暴漏其他模块
bootstrap: [],
})
export class AppModule {}
- 全局模块 返回
@NgModule({
declarations: [], // 组件、指令、指令
exports: [], // 组件、指令、指令
})
export class SharedModule {}
视图组件
- 定义组件 返回
import { Component } from '@angular/core';
@Component({
selector,
templateUrl,
providers, // @Injectable 上下文服务
styleUrls,
})
export class Component {
constructor(
private provider: Injectable // @Injectable 上下文服务
){}
}
- 组件数据接收 返回
import { Component, Input } from '@angular/core';
@Component(...)
export class Child {
@Input() type: String;
}
......
<child [type]="Hello !"/>
- 模板语法 返回
// 1.组件模板 <= 组件类的数据
<p>{{ name }}</p>
// 2.组件模板 <= 组件事件
<p (click)="method('hello!')"></p>
// 3.组件模板 <=> 组件类的数据
// <input [(ngModel)]="name" />
// 4.模板 if 指令
// <div *ngIf="Boolean; else IdSelector">Editorable!</div>
// <div #IdSelector>NoEditorable</div>
// 5.模板 for 指令
// <li *ngFor="let user of users; index as i; first as isFirst"></li>
// index: number:可迭代对象中当前条目的索引。
// count: number:可迭代对象的长度。
// first: boolean:如果当前条目是可迭代对象中的第一个条目则为 true。
// last: boolean:如果当前条目是可迭代对象中的最后一个条目则为 true。
// even: boolean:如果当前条目在可迭代对象中的索引号为偶数则为 true。
// odd: boolean:如果当前条目在可迭代对象中的索引号为奇数则为 true。
- 向父组件发送数据 返回
import { Component, Output } from '@angular/core';
@Component(...)
export class Child {
@Output() messager = new EventEmitter<Type>();
sendMessage(any: Type) {
this.messager.emit(any);
}
}
......
<child (messager)="next($event)"/>
管道
// 1.内置管道
<h1>{{key | uppercase}} Details</h1>
路由
- 返回
(1/)定义路由
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [{ path, component }];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
(2/)注册路由
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module'; // CLI imports AppRoutingModule
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
上下文服务
- 声明服务 返回
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class SomeService {
constructor(private http: HttpClient) { }
}
- 注册服务到(模块/组件) 返回
import { NgModule } from '@angular/core';
@NgModule({
providers: [
SomeService,
],
})
export class AppModule {}
零部件生命周期
- ngOnChanges - ngOnInit - ngDoCheck
- ngAfterContentInit - ngAfterContentChecked
- ngAfterViewInit - ngAfterViewChecked
- ngOnDestroy
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!