SlideShare ist ein Scribd-Unternehmen logo
1 von 20
按一下以編輯母片標題樣式
按一下以編輯母片子標題樣式
我是[黃保翕]
Will 保哥
多奇數位創意有限公司 技術總監
https://blog.miniasp.com
Find me at
https://www.facebook.com/will.fans
按一下以編輯母片標題樣式
按一下以編輯母片子標題樣式
Micro-frontend with
Angular 10
Will 保哥
Micro-frontends with Angular 10
使用 Angular 10 實現微前端
Implementing Micro-frontends with Angular 10
將元件整合進不同的前端框架
Integrate Angular 10 components into other frameworks
何謂微前端
What is Micro-frontends?
Micro-frontends with Angular 10
使用 Angular 10 實現微前端
Implementing Micro-frontends with Angular 10
將元件整合進不同的前端框架Integrate Angular 10 components into other frameworks
何謂微前端
What is Micro-frontends?
微前端 (Micro Frontends)
• 最早出現於 2016 年 ThoughtWorks 的技術雷達
• 借用 微服務架構 (Microservices) 許多概念與作法
• 有效解構大型前端架構下的各種相依、協作、整合等問題
• Be Technology Agnostic(不限定各團隊所採用的前端技術)
• Isolate Team Code(不同的團隊代碼必須彼此隔離以降低相依性)
• Establish Team Prefixes(建立團隊之間的命名規則以降低資源衝突)
• Favor Native Browser Features over Custom APIs(使用原生 API 為
主)
• Build a Resilient Site(建置有高度彈性、遇到錯誤可迅速回復的站台)
將頁面分割為不同的 Web 元件
https://www.toptal.com/front-end/micro-frontends-strengths-benefits
套用微前端架構的 HTML 結構範例
可動態載入外部 JS 檔案
自訂元素 (Custom Elements)
Custom Elements (V1)
Micro-frontends with Angular 10
使用 Angular 10 實現微前端
Implementing Micro-frontends with Angular 10
將元件整合進不同的前端框架Integrate Angular 10 components into other frameworks
何謂微前端
What is Micro-frontends?
初始化專案
• ng new --create-application=false mfdemo1 && cd mfdemo1
• ng g application mfdemo1 --routing --style=css
• ng g application mf-element1 --routing=false --style=css
• ng add @angular/elements --project=mf-element1
• ng add ngx-build-plus --project=mf-element1
移除 AppComponent 的 selector 屬性
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'mf-element1';
}
移除 AppModule 的 bootstrap 屬性
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule
],
providers: []
, bootstrap: [AppComponent]
})
export class AppModule { … }
加入 AppModule 的 ngDoBootstrap()
import { Injector } from '@angular/core';
import { createCustomElement } from '@angular/elements';
@NgModule(…)
export class AppModule {
constructor(private injector: Injector) {}
ngDoBootstrap(): void {
const customElement = createCustomElement(AppComponent, { injector: this.injector });
customElements.define('mf-element1', customElement);
}
}
關於 Polyfills 設定
• 使用於 non-Angular 網頁環境需額外載入 zone.js 才能執行
• node_modules/zone.js/dist/zone.min.js
• 針對不支援 Custom Elements (V1) 的瀏覽器提供 Polyfills
(IE11)
• 在執行 ng add @angular/elements 的時候會自動加入 document-register-element 套件
• node_modules/document-register-element/build/document-register-element.js
• 其他可能需要的 Polyfills 套件
• ng g ngx-build-plus:wc-polyfill --project mf-element1
• npm install core-js@2
建置專案
• ng build --project=mfdemo1 --prod --output-hashing=none
• ng build --project=mf-element1 --prod --output-hashing=none --single-bundle
mfdemo1 mf-element1 說明
3rdpartylicenses.txt 3rdpartylicenses.txt 所有套件的授權聲明
favicon.ico favicon.ico 網站的 Favicon 圖檔
index.html index.html 網站預設首頁
main.js main.js 主要程式
runtime.js 內容被合併至 main.js 執行 Angular 所需的共用程式
polyfills.js polyfills.js 支援舊版瀏覽器所需的 Polyfills
styles.css styles.css 全站共用的 CSS 內容
部署 Angular Elements 與 Polyfills
• 複製主程式與 Polyfills
• cp dist/mf-element1/main.js dist/mfdemo1/mf-element1.js
• cp dist/mf-element1/polyfills.js dist/mfdemo1/mf-polyfills.js
• 僅複製必要的 zone.js 與 document-register-element.js
• cp node_modules/zone.js/dist/zone.min.js dist/mf-element1/zone.min.js
• cp node_modules/document-register-element/build/document-register-element.js dist/mf-
element1/document-register-element.js
• 注意事項:請不要在 main.ts 匯入 zone.js
• import 'zone.js/dist/zone';
• Configuring CommonJS dependencies
Micro-frontends with Angular 10
使用 Angular 10 實現微前端
Implementing Micro-frontends with Angular 10
將元件整合進不同的前端框架Integrate Angular 10 components into other frameworks
何謂微前端
What is Micro-frontends?
非 Angular 網頁嵌入 Angular Elements
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Mfdemo1</title>
</head>
<body>
<mf-element1></mf-element1>
<script src="mf-polyfills.js" defer></script>
<script src="mf-element1.js" defer></script>
</body>
</html>
非 Angular 網頁嵌入 Angular Elements
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Mfdemo1</title>
</head>
<body>
<mf-element1></mf-element1>
<script src="zone.min.js" defer></script>
<script src="mf-element1.js" defer></script>
</body>
</html>
從 Angular 載入 Angular Elements
• 安裝 Angular Extensions Elements 套件
• npm i @angular-extensions/elements
• 匯入 LazyElementsModule 至 AppModule 的 imports 屬性
• import { LazyElementsModule } from '@angular-extensions/elements';
• 加入 CUSTOM_ELEMENTS_SCHEMA 至 AppModule 的 schemas 屬性
• schemas: [CUSTOM_ELEMENTS_SCHEMA]
• 使用 *axLazyElement 結構型指令
• <mf-element1 *axLazyElement="'/mf-element1.js'"></mf-element1>
• The Best Way To Lazy Load Angular Elements | by Tomas Trajan
• Angular Elements Demo
按一下以編輯母片標題樣式
按一下以編輯母片子標題樣式
歡迎將
講者照片
裁成圓形
置於此處
我是[黃保翕]
Will 保哥
多奇數位創意有限公司 技術總監
https://blog.miniasp.com
Find me at
https://www.facebook.com/will.fans

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

"Yahoo! JAPAN の Kubernetes-as-a-Service" で加速するアプリケーション開発
"Yahoo! JAPAN の Kubernetes-as-a-Service" で加速するアプリケーション開発"Yahoo! JAPAN の Kubernetes-as-a-Service" で加速するアプリケーション開発
"Yahoo! JAPAN の Kubernetes-as-a-Service" で加速するアプリケーション開発
 
與大師對談: 轉移到微服務架構必經之路 ~ 系統與資料庫重構
與大師對談: 轉移到微服務架構必經之路~ 系統與資料庫重構與大師對談: 轉移到微服務架構必經之路~ 系統與資料庫重構
與大師對談: 轉移到微服務架構必經之路 ~ 系統與資料庫重構
 
De 0 à Angular en 1h30! (french)
De 0 à Angular en 1h30! (french)De 0 à Angular en 1h30! (french)
De 0 à Angular en 1h30! (french)
 
Azure DevOps と開発管理
Azure DevOps と開発管理Azure DevOps と開発管理
Azure DevOps と開発管理
 
Modern web 2020 - 使用 Nx 管理超大型前後端專案
Modern web 2020 - 使用 Nx 管理超大型前後端專案Modern web 2020 - 使用 Nx 管理超大型前後端專案
Modern web 2020 - 使用 Nx 管理超大型前後端專案
 
これを覚えたら表現力100倍 - Flex MessageとQuick Reply
これを覚えたら表現力100倍 - Flex MessageとQuick Replyこれを覚えたら表現力100倍 - Flex MessageとQuick Reply
これを覚えたら表現力100倍 - Flex MessageとQuick Reply
 
twMVC#44 讓我們用 k6 來進行壓測吧
twMVC#44 讓我們用 k6 來進行壓測吧twMVC#44 讓我們用 k6 來進行壓測吧
twMVC#44 讓我們用 k6 來進行壓測吧
 
Spring 2.0 技術手冊第一章 - 認識 Spring
Spring 2.0 技術手冊第一章 - 認識 SpringSpring 2.0 技術手冊第一章 - 認識 Spring
Spring 2.0 技術手冊第一章 - 認識 Spring
 
Jenkins 再入門
Jenkins 再入門Jenkins 再入門
Jenkins 再入門
 
例外處理實務
例外處理實務例外處理實務
例外處理實務
 
Java Web 程式之效能技巧與安全防護
Java Web 程式之效能技巧與安全防護Java Web 程式之效能技巧與安全防護
Java Web 程式之效能技巧與安全防護
 
組み込みブラウザとメディアﰀ仕様あれこれ
組み込みブラウザとメディアﰀ仕様あれこれ組み込みブラウザとメディアﰀ仕様あれこれ
組み込みブラウザとメディアﰀ仕様あれこれ
 
安全なWebアプリケーションの作り方2018
安全なWebアプリケーションの作り方2018安全なWebアプリケーションの作り方2018
安全なWebアプリケーションの作り方2018
 
エンタープライズIT環境での OpenID Connect / SCIM の具体的実装方法 idit2014
エンタープライズIT環境での OpenID Connect / SCIM の具体的実装方法 idit2014エンタープライズIT環境での OpenID Connect / SCIM の具体的実装方法 idit2014
エンタープライズIT環境での OpenID Connect / SCIM の具体的実装方法 idit2014
 
使用 laravel 的前與後
使用 laravel 的前與後使用 laravel 的前與後
使用 laravel 的前與後
 
Djangoフレームワークのユーザーモデルと認証
Djangoフレームワークのユーザーモデルと認証Djangoフレームワークのユーザーモデルと認証
Djangoフレームワークのユーザーモデルと認証
 
Introduction à Angular
Introduction à AngularIntroduction à Angular
Introduction à Angular
 
入社1年目のプログラミング初心者がSpringを学ぶための手引き
入社1年目のプログラミング初心者がSpringを学ぶための手引き入社1年目のプログラミング初心者がSpringを学ぶための手引き
入社1年目のプログラミング初心者がSpringを学ぶための手引き
 
MySQLの限界に挑戦する
MySQLの限界に挑戦するMySQLの限界に挑戦する
MySQLの限界に挑戦する
 
微服務架構|01|入門微服務|到底什麼是微服務?
微服務架構|01|入門微服務|到底什麼是微服務?微服務架構|01|入門微服務|到底什麼是微服務?
微服務架構|01|入門微服務|到底什麼是微服務?
 

Ähnlich wie Micro-frontends with Angular 10 (Modern Web 2020)

Koubei banquet 27
Koubei banquet 27Koubei banquet 27
Koubei banquet 27
Koubei UED
 
使用Javascript及HTML5打造協同運作系統
使用Javascript及HTML5打造協同運作系統使用Javascript及HTML5打造協同運作系統
使用Javascript及HTML5打造協同運作系統
Hsu Ping Feng
 
Clojure cnclojure-meetup
Clojure cnclojure-meetupClojure cnclojure-meetup
Clojure cnclojure-meetup
sunng87
 
企業導入 Angular 作為前端開發的好處
企業導入 Angular 作為前端開發的好處企業導入 Angular 作為前端開發的好處
企業導入 Angular 作為前端開發的好處
Oomusou Xiao
 

Ähnlich wie Micro-frontends with Angular 10 (Modern Web 2020) (20)

Angular 7 全新功能探索 (Angular Taiwan 2018)
Angular 7 全新功能探索 (Angular Taiwan 2018)Angular 7 全新功能探索 (Angular Taiwan 2018)
Angular 7 全新功能探索 (Angular Taiwan 2018)
 
Angular 4 網站開發最佳實務 (Modern Web 2017)
Angular 4 網站開發最佳實務 (Modern Web 2017)Angular 4 網站開發最佳實務 (Modern Web 2017)
Angular 4 網站開發最佳實務 (Modern Web 2017)
 
Koubei banquet 27
Koubei banquet 27Koubei banquet 27
Koubei banquet 27
 
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)
 
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
 
Angular 5 全新功能探索 @ JSDC2017
Angular 5 全新功能探索 @ JSDC2017Angular 5 全新功能探索 @ JSDC2017
Angular 5 全新功能探索 @ JSDC2017
 
Better use angular
Better use angularBetter use angular
Better use angular
 
20141212 html5 及微軟跨平台佈局 long
20141212 html5 及微軟跨平台佈局   long20141212 html5 及微軟跨平台佈局   long
20141212 html5 及微軟跨平台佈局 long
 
移动Web开发框架jqm探讨
移动Web开发框架jqm探讨移动Web开发框架jqm探讨
移动Web开发框架jqm探讨
 
使用Javascript及HTML5打造協同運作系統
使用Javascript及HTML5打造協同運作系統使用Javascript及HTML5打造協同運作系統
使用Javascript及HTML5打造協同運作系統
 
Insider Dev Tour - Taipei Productive and Fun Web Development with NodeJS and ...
Insider Dev Tour - Taipei Productive and Fun Web Development with NodeJS and ...Insider Dev Tour - Taipei Productive and Fun Web Development with NodeJS and ...
Insider Dev Tour - Taipei Productive and Fun Web Development with NodeJS and ...
 
WoT Frotend 的設計與實作
WoT Frotend 的設計與實作WoT Frotend 的設計與實作
WoT Frotend 的設計與實作
 
2021laravelconftwslides8
2021laravelconftwslides82021laravelconftwslides8
2021laravelconftwslides8
 
Phalcon2014 Startup
Phalcon2014 StartupPhalcon2014 Startup
Phalcon2014 Startup
 
Clojure cnclojure-meetup
Clojure cnclojure-meetupClojure cnclojure-meetup
Clojure cnclojure-meetup
 
ASP.NET MVC 6 新功能探索
ASP.NET MVC 6 新功能探索ASP.NET MVC 6 新功能探索
ASP.NET MVC 6 新功能探索
 
twMVC#17 | 使用Angular.js開發大型系統架構
twMVC#17 | 使用Angular.js開發大型系統架構twMVC#17 | 使用Angular.js開發大型系統架構
twMVC#17 | 使用Angular.js開發大型系統架構
 
极速 Angular 开发:效能调校技巧 (ngChina 2019)
极速 Angular 开发:效能调校技巧 (ngChina 2019)极速 Angular 开发:效能调校技巧 (ngChina 2019)
极速 Angular 开发:效能调校技巧 (ngChina 2019)
 
企業導入 Angular 作為前端開發的好處
企業導入 Angular 作為前端開發的好處企業導入 Angular 作為前端開發的好處
企業導入 Angular 作為前端開發的好處
 
CodeIgniter 2.0.X
CodeIgniter 2.0.XCodeIgniter 2.0.X
CodeIgniter 2.0.X
 

Mehr von Will Huang

Mehr von Will Huang (20)

深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)
深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)
深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)
 
從頭打造 C#、.NET 與 ASP.NET Core 開發環境
從頭打造 C#、.NET 與 ASP.NET Core 開發環境從頭打造 C#、.NET 與 ASP.NET Core 開發環境
從頭打造 C#、.NET 與 ASP.NET Core 開發環境
 
你一定不能不知道的 Markdown 寫作技巧
你一定不能不知道的 Markdown 寫作技巧你一定不能不知道的 Markdown 寫作技巧
你一定不能不知道的 Markdown 寫作技巧
 
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)
 
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)
 
從實戰經驗看到的 K8S 導入痛點
從實戰經驗看到的 K8S 導入痛點從實戰經驗看到的 K8S 導入痛點
從實戰經驗看到的 K8S 導入痛點
 
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
 
Protractor: The Hacker way (NG-MY 2019)
Protractor: The Hacker way (NG-MY 2019)Protractor: The Hacker way (NG-MY 2019)
Protractor: The Hacker way (NG-MY 2019)
 
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
 
Angular 开发技巧 (2018 ngChina 开发者大会)
Angular 开发技巧 (2018 ngChina 开发者大会)Angular 开发技巧 (2018 ngChina 开发者大会)
Angular 开发技巧 (2018 ngChina 开发者大会)
 
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
 
AKS 與開發人員體驗 (Kubernetes 大講堂)
AKS 與開發人員體驗 (Kubernetes 大講堂)AKS 與開發人員體驗 (Kubernetes 大講堂)
AKS 與開發人員體驗 (Kubernetes 大講堂)
 
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
 
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
全新 Windows Server 2019 容器技術及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)全新 Windows Server 2019 容器技術及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
 
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
 
以敏捷架構打造美國軟體外包專案的經驗談
以敏捷架構打造美國軟體外包專案的經驗談以敏捷架構打造美國軟體外包專案的經驗談
以敏捷架構打造美國軟體外包專案的經驗談
 
開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018
開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018
開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018
 
迎接嶄新的Windows容器叢集架構:Kubernetes
迎接嶄新的Windows容器叢集架構:Kubernetes迎接嶄新的Windows容器叢集架構:Kubernetes
迎接嶄新的Windows容器叢集架構:Kubernetes
 
TypeScript 綜合格鬥技
TypeScript 綜合格鬥技TypeScript 綜合格鬥技
TypeScript 綜合格鬥技
 
Windows Container 101: dotNET, Container, Kubernetes
Windows Container 101: dotNET, Container, KubernetesWindows Container 101: dotNET, Container, Kubernetes
Windows Container 101: dotNET, Container, Kubernetes
 

Micro-frontends with Angular 10 (Modern Web 2020)

  • 3. Micro-frontends with Angular 10 使用 Angular 10 實現微前端 Implementing Micro-frontends with Angular 10 將元件整合進不同的前端框架 Integrate Angular 10 components into other frameworks 何謂微前端 What is Micro-frontends?
  • 4. Micro-frontends with Angular 10 使用 Angular 10 實現微前端 Implementing Micro-frontends with Angular 10 將元件整合進不同的前端框架Integrate Angular 10 components into other frameworks 何謂微前端 What is Micro-frontends?
  • 5. 微前端 (Micro Frontends) • 最早出現於 2016 年 ThoughtWorks 的技術雷達 • 借用 微服務架構 (Microservices) 許多概念與作法 • 有效解構大型前端架構下的各種相依、協作、整合等問題 • Be Technology Agnostic(不限定各團隊所採用的前端技術) • Isolate Team Code(不同的團隊代碼必須彼此隔離以降低相依性) • Establish Team Prefixes(建立團隊之間的命名規則以降低資源衝突) • Favor Native Browser Features over Custom APIs(使用原生 API 為 主) • Build a Resilient Site(建置有高度彈性、遇到錯誤可迅速回復的站台)
  • 7. 套用微前端架構的 HTML 結構範例 可動態載入外部 JS 檔案 自訂元素 (Custom Elements) Custom Elements (V1)
  • 8. Micro-frontends with Angular 10 使用 Angular 10 實現微前端 Implementing Micro-frontends with Angular 10 將元件整合進不同的前端框架Integrate Angular 10 components into other frameworks 何謂微前端 What is Micro-frontends?
  • 9. 初始化專案 • ng new --create-application=false mfdemo1 && cd mfdemo1 • ng g application mfdemo1 --routing --style=css • ng g application mf-element1 --routing=false --style=css • ng add @angular/elements --project=mf-element1 • ng add ngx-build-plus --project=mf-element1
  • 10. 移除 AppComponent 的 selector 屬性 import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'mf-element1'; }
  • 11. 移除 AppModule 的 bootstrap 屬性 @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule ], providers: [] , bootstrap: [AppComponent] }) export class AppModule { … }
  • 12. 加入 AppModule 的 ngDoBootstrap() import { Injector } from '@angular/core'; import { createCustomElement } from '@angular/elements'; @NgModule(…) export class AppModule { constructor(private injector: Injector) {} ngDoBootstrap(): void { const customElement = createCustomElement(AppComponent, { injector: this.injector }); customElements.define('mf-element1', customElement); } }
  • 13. 關於 Polyfills 設定 • 使用於 non-Angular 網頁環境需額外載入 zone.js 才能執行 • node_modules/zone.js/dist/zone.min.js • 針對不支援 Custom Elements (V1) 的瀏覽器提供 Polyfills (IE11) • 在執行 ng add @angular/elements 的時候會自動加入 document-register-element 套件 • node_modules/document-register-element/build/document-register-element.js • 其他可能需要的 Polyfills 套件 • ng g ngx-build-plus:wc-polyfill --project mf-element1 • npm install core-js@2
  • 14. 建置專案 • ng build --project=mfdemo1 --prod --output-hashing=none • ng build --project=mf-element1 --prod --output-hashing=none --single-bundle mfdemo1 mf-element1 說明 3rdpartylicenses.txt 3rdpartylicenses.txt 所有套件的授權聲明 favicon.ico favicon.ico 網站的 Favicon 圖檔 index.html index.html 網站預設首頁 main.js main.js 主要程式 runtime.js 內容被合併至 main.js 執行 Angular 所需的共用程式 polyfills.js polyfills.js 支援舊版瀏覽器所需的 Polyfills styles.css styles.css 全站共用的 CSS 內容
  • 15. 部署 Angular Elements 與 Polyfills • 複製主程式與 Polyfills • cp dist/mf-element1/main.js dist/mfdemo1/mf-element1.js • cp dist/mf-element1/polyfills.js dist/mfdemo1/mf-polyfills.js • 僅複製必要的 zone.js 與 document-register-element.js • cp node_modules/zone.js/dist/zone.min.js dist/mf-element1/zone.min.js • cp node_modules/document-register-element/build/document-register-element.js dist/mf- element1/document-register-element.js • 注意事項:請不要在 main.ts 匯入 zone.js • import 'zone.js/dist/zone'; • Configuring CommonJS dependencies
  • 16. Micro-frontends with Angular 10 使用 Angular 10 實現微前端 Implementing Micro-frontends with Angular 10 將元件整合進不同的前端框架Integrate Angular 10 components into other frameworks 何謂微前端 What is Micro-frontends?
  • 17. 非 Angular 網頁嵌入 Angular Elements <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Mfdemo1</title> </head> <body> <mf-element1></mf-element1> <script src="mf-polyfills.js" defer></script> <script src="mf-element1.js" defer></script> </body> </html>
  • 18. 非 Angular 網頁嵌入 Angular Elements <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Mfdemo1</title> </head> <body> <mf-element1></mf-element1> <script src="zone.min.js" defer></script> <script src="mf-element1.js" defer></script> </body> </html>
  • 19. 從 Angular 載入 Angular Elements • 安裝 Angular Extensions Elements 套件 • npm i @angular-extensions/elements • 匯入 LazyElementsModule 至 AppModule 的 imports 屬性 • import { LazyElementsModule } from '@angular-extensions/elements'; • 加入 CUSTOM_ELEMENTS_SCHEMA 至 AppModule 的 schemas 屬性 • schemas: [CUSTOM_ELEMENTS_SCHEMA] • 使用 *axLazyElement 結構型指令 • <mf-element1 *axLazyElement="'/mf-element1.js'"></mf-element1> • The Best Way To Lazy Load Angular Elements | by Tomas Trajan • Angular Elements Demo

Hinweis der Redaktion

  1. npm i @webcomponents/custom-elements npm i core-js@2
  2. https://github.com/manfredsteyer/ngx-build-plus/issues/4