SlideShare a Scribd company logo
4
Most read
6
Most read
10
Most read
使用 Standalone Component
來寫 Angular 吧!
Mike Huang
多奇數位創意有限公司
About Me
● Title
○ 多奇數位創意有限公司
○ Angular GDE
○ Microsoft MVP
● Awards
○ 2018 iT 邦幫忙鐵人賽 冠軍
○ 2019 iT 邦幫忙鐵人賽 優選
○ 第 12 屆 iThome 鐵人賽 冠軍
● 著作:打通 RxJS 任督二脈
https://github.com/wellwind
https://www.facebook.com/fullstackledder
https://fullstackladder.dev
https://www.tenlong.com.tw/products/9789864348039
先回憶一下使用
@NgModule 的感覺
@NgModule({
declarations: [
AppComponent,
LayoutComponent
],
imports: [
BrowserModule,
AnotherModule,
TodoListModule
],
providers: [ ],
bootstrap: [AppComponent]
})
export class AppModule { }
@NgModule({
declarations: [
TodoListComponent,
TodoItemComponent,
TodoTextPipe,
TodoDoneDirective
],
imports: [
CommonModule
],
exports: [
TodoListComponent
]
})
export class TodoListModule { }
@Component({
selector: 'app-root',
template: '<app-todo-list></app-todo-list>'
})
export class AppComponent { }
如何找到 <app-todo-list></app-todo-list> 元件?
@Component({
selector: 'app-root',
template: '<app-todo-list></app-todo-list>'
})
export class AppComponent { }
如何找到 <app-todo-list></app-todo-list> 元件?
@NgModule({
declarations: [
AppComponent,
LayoutComponent
],
imports: [
BrowserModule,
AnotherModule,
TodoListModule
],
providers: [ ],
bootstrap: [AppComponent]
})
export class AppModule { }
1
2
@NgModule({
declarations: [
TodoListComponent,
TodoItemComponent,
TodoTextPipe,
TodoDoneDirective
],
imports: [
CommonModule
],
exports: [
TodoListComponent
]
})
export class TodoListModule { }
3
4
用 Standalone Component 來寫 Angular 吧! - STUDY4.TW 2023 小聚 - 聊前端
Angular Standalone
Components / Directives / Pipes
Standalone Components / Directives / Pipes
● Angular 14 推出 Developer Preview
● Angular 15 正式宣告 stable
● 在 @Component、@Directive、@Pipe 加上 standalone: true 即可!
● Standalone 程式本身即可用 imports: [] 匯入
○ 其他的 @NgModule
○ 其他的 Standalone 程式
● 有 @NgModule 的概念,但又不用管 @NgModule
● 提供 CLI 工具快速轉換與產生程式
● 更加直覺、簡單的開發方式
● 打包速度更快、產出檔案更小
@Component({
standalone: true,
selector: 'app-root',
...
})
export class AppComponent { }
• 加上 standalone: true 就對了!
• 在 @Directive / @Pipe 上一樣適用
@Component({
standalone: true,
selector: 'app-todo-list',
imports: [
CommonModule
],
template: `
<ul *ngFor="...">
<li>...</li>
</ul>
`
})
export class TodoListComponent { }
@Component({
standalone: true,
selector: 'app-root',
...
})
export class AppComponent { }
準備一個 standalone component
ng generate component [name] --standalone
imports: [] 可加入其他 @NgModule
@Component({
standalone: true,
selector: 'app-root',
imports: [
NgIf
TodoListComponent
],
template: `
<app-todo-list>
</app-todo-list>
`
})
export class AppComponent { }
@Component({
standalone: true,
selector: 'app-todo-list',
imports: [
CommonModule
],
template: `
<ul *ngFor="...">
<li>...</li>
</ul>
`
})
export class TodoListComponent { }
NgIf、NgFor 等 directives 現在也都是 standalone 了!
@Component({
standalone: true,
selector: 'app-root',
imports: [
TodoListComponent
],
template: `
<app-todo-list>
</app-todo-list>
`
})
export class AppComponent { }
@Component({
standalone: true,
selector: 'app-todo-list',
imports: [
CommonModule
],
template: `
<ul *ngFor="...">
<li>...</li>
</ul>
`
})
export class TodoListComponent { }
standalone component 可以放入
另外一個 standalone component 的 imoprts: [] 中
@Component({
standalone: true,
selector: 'app-root',
imports: [
NgIf,
TodoListComponent
],
template: `
<app-todo-list *ngIf="...">
</app-todo-list>
`
})
export class AppComponent { }
如何找到 <app-todo-list></app-todo-list> 元件?
1 抬頭就找到!
使用 Angular CLI 快速將專案轉換成 Standalone
重要:請將專案與 Angular CLI 都升級到 15.2.0 以上
ng generate @angular/core:standalone
• Convert all components, directives and pipes to standalone
• Remove unnecessary NgModule classes
• Bootstrap the application using standalone APIs
• 將所有 components, directives 和 pipes 轉換成 standalone
• 移除所有用不到的 NgModule 類別
• 使用 standalone APIs 啟動應用程式
更多重點都在文件裡
https://angular.io/guide/standalone-components
DEMO
Resources
● 搶先體驗 Standalone Components / Directives / Pipes
● RFC: Standalone APIs
● Getting started with standalone components

More Related Content

PPTX
Akka actorを何故使うのか?
PDF
Spring Boot × Vue.jsでSPAを作る
PDF
使用 Passkeys 打造無密碼驗證服務
PDF
實踐 Clean Architecture(實作高可用性的軟件架構)
PPTX
Introduction to jenkins
PDF
TypeProf for IDE: Enrich Development Experience without Annotations
PDF
PDF
從零開始做架構圖
Akka actorを何故使うのか?
Spring Boot × Vue.jsでSPAを作る
使用 Passkeys 打造無密碼驗證服務
實踐 Clean Architecture(實作高可用性的軟件架構)
Introduction to jenkins
TypeProf for IDE: Enrich Development Experience without Annotations
從零開始做架構圖

What's hot (20)

PPTX
PDF
Reactive Programming for a demanding world: building event-driven and respons...
PDF
"Simple Made Easy" Made Easy
PDF
Domain Driven Design with the F# type System -- F#unctional Londoners 2014
PDF
PEGで構文解析をする
PDF
導入から 10 年、PHP の trait は滅びるべきなのか その適切な使いどころと弱点、将来について
PDF
The Usage and Patterns of MagicOnion
PDF
Step by Step Guide on Lazy Loading in Angular 11
PDF
Angular Taiwan 2019 - 大型 Angular 專案的的管理心得與技巧
PPTX
リアルタイムサーバー 〜Erlang/OTPで作るPubSubサーバー〜
PDF
GraphQLでフロントエンドの複雑性とたたかう
PDF
WebRTC入門+最新動向
PPTX
明日からはじめられる Docker + さくらvpsを使った開発環境構築
PPTX
SPAセキュリティ入門~PHP Conference Japan 2021
PDF
Kotlinアンチパターン
PDF
introduction to Vue.js 3
PPTX
Intro to Docker November 2013
PDF
劇的改善 Ci4時間から5分へ〜私がやった10のこと〜
PDF
CI/CDツール比較してみた
PDF
Enforcing Bespoke Policies in Kubernetes
Reactive Programming for a demanding world: building event-driven and respons...
"Simple Made Easy" Made Easy
Domain Driven Design with the F# type System -- F#unctional Londoners 2014
PEGで構文解析をする
導入から 10 年、PHP の trait は滅びるべきなのか その適切な使いどころと弱点、将来について
The Usage and Patterns of MagicOnion
Step by Step Guide on Lazy Loading in Angular 11
Angular Taiwan 2019 - 大型 Angular 專案的的管理心得與技巧
リアルタイムサーバー 〜Erlang/OTPで作るPubSubサーバー〜
GraphQLでフロントエンドの複雑性とたたかう
WebRTC入門+最新動向
明日からはじめられる Docker + さくらvpsを使った開発環境構築
SPAセキュリティ入門~PHP Conference Japan 2021
Kotlinアンチパターン
introduction to Vue.js 3
Intro to Docker November 2013
劇的改善 Ci4時間から5分へ〜私がやった10のこと〜
CI/CDツール比較してみた
Enforcing Bespoke Policies in Kubernetes
Ad

More from 升煌 黃 (13)

PDF
陽明交大 - 跟著 AI 學習 Angular
PDF
.NET Conf Taiwan 2022 - Tauri - 前端人員也能打造小巧快速的 Windows 應用程式
PDF
DevFest 2022 Taipei 使用 Standalone Component 來寫 Angular 吧!
PDF
gRPC - 打造輕量、高效能的後端服務
PDF
Modern web 2020 - 使用 Nx 管理超大型前後端專案
PDF
Angular Taiwan 2019 - Schematics Workshop
PDF
Angular Meetup 04 - Angular PWA 之沒有網路的日子 20190731
PDF
Angular Taiwan 2018 - Angular CDK
PPTX
玩轉 Schematics - Modern Web 2018
PPTX
OAuth2介紹
PPTX
Docker - 30秒生出100台伺服器
PPTX
敏捷開發與Scrum
PPTX
Test Driven Development
陽明交大 - 跟著 AI 學習 Angular
.NET Conf Taiwan 2022 - Tauri - 前端人員也能打造小巧快速的 Windows 應用程式
DevFest 2022 Taipei 使用 Standalone Component 來寫 Angular 吧!
gRPC - 打造輕量、高效能的後端服務
Modern web 2020 - 使用 Nx 管理超大型前後端專案
Angular Taiwan 2019 - Schematics Workshop
Angular Meetup 04 - Angular PWA 之沒有網路的日子 20190731
Angular Taiwan 2018 - Angular CDK
玩轉 Schematics - Modern Web 2018
OAuth2介紹
Docker - 30秒生出100台伺服器
敏捷開發與Scrum
Test Driven Development
Ad

用 Standalone Component 來寫 Angular 吧! - STUDY4.TW 2023 小聚 - 聊前端