Micro Frontends Architecture with Module Federation · บทเรียน

การสื่อสารด้วยเหตุการณ์ DOM แบบกำหนดเอง

เรียนรู้การใช้ CustomEvent API ดั้งเดิมของเบราว์เซอร์เป็นช่องทางการสื่อสารที่มีน้ำหนักเบาและไม่ผูกกับเฟรมเวิร์ก ระหว่างไมโครฟรอนต์เอนด์

บทเรียน 4 จาก 413 ขั้นตอน

การสื่อสารด้วยเหตุการณ์ DOM แบบกำหนดเอง เป็นบทเรียน Micro Frontends Architecture with Module Federation ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Micro Frontends Architecture with Module Federation และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Micro Frontends Architecture with Module Federation มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Native Events?

The browser already has a built-in pub/sub system: the DOM event model. Using it for MFE communication needs no library and works across any framework.

The CustomEvent API

CustomEvent lets you create your own named events carrying a detail payload, dispatched on any DOM element including window.

const evt = new CustomEvent("cart:add", {
  detail: { productId: 42, qty: 1 }
});

Dispatching an Event

Any micro frontend can broadcast an event on a shared target such as window. Others do not need a direct reference to the sender.

window.dispatchEvent(new CustomEvent("cart:add", {
  detail: { productId: 42 }
}));

Listening for Events

A consuming MFE subscribes with addEventListener and reads the payload from event.detail.

window.addEventListener("cart:add", (e) => {
  updateCart(e.detail.productId);
});

Naming Conventions

Namespace event names to avoid collisions, for example cart:add or auth:login. A shared naming convention is part of your communication contract.

Cleaning Up Listeners

When a micro frontend unmounts, remove its listeners to prevent memory leaks and duplicate handling.

const handler = (e) => updateCart(e.detail);
window.addEventListener("cart:add", handler);
// on unmount:
window.removeEventListener("cart:add", handler);

Two-Way Communication

For request/response style flows, one MFE dispatches a request event and another replies with a separate response event, keeping each direction explicit.

window.dispatchEvent(new CustomEvent("user:request"));
window.addEventListener("user:response", e => show(e.detail));

Strengths of DOM Events

Native events are appealing because they are:

  • Framework-agnostic
  • Zero-dependency
  • Familiar to all web developers
  • Naturally decoupled (no shared references)

Limitations to Know

They also have downsides:

  • No type safety on detail
  • Easy to misspell event names
  • Hard to trace who listens to what
  • Not ideal for large or frequent payloads

Wrapping Events in a Typed Helper

To reduce mistakes, wrap dispatch and listen in a small typed module shared across MFEs, centralizing event names and payload shapes.

export function emit(name, detail) {
  window.dispatchEvent(new CustomEvent(name, { detail }));
}

When to Choose This Pattern

Custom DOM events shine for simple, occasional notifications between loosely coupled MFEs. For complex shared data, prefer a dedicated event bus or shared store.

Quick Check

Test your custom-event knowledge.

Recap

You learned native event communication:

  • CustomEvent carries data in detail
  • Dispatch on window; listen with addEventListener
  • Namespace names and clean up listeners
  • Great for simple, decoupled notifications
  • Wrap in a typed helper to avoid mistakes

DOM events are a zero-dependency MFE channel.

เริ่มต้นได้ฟรี

เรียนรู้ JavaScript ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การสื่อสารด้วยเหตุการณ์ DOM แบบกำหนดเอง” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การสื่อสารด้วยเหตุการณ์ DOM แบบกำหนดเอง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Micro Frontends Architecture with Module Federation ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Micro Frontends Architecture with Module Federation มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การสื่อสารด้วยเหตุการณ์ DOM แบบกำหนดเอง”

เรียนรู้การใช้ CustomEvent API ดั้งเดิมของเบราว์เซอร์เป็นช่องทางการสื่อสารที่มีน้ำหนักเบาและไม่ผูกกับเฟรมเวิร์ก ระหว่างไมโครฟรอนต์เอนด์ คุณปฏิบัติ Micro Frontends Architecture with Module Federation ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Micro Frontends Architecture with Module Federation หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Micro Frontends Architecture with Module Federation บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การสื่อสารด้วยเหตุการณ์ DOM แบบกำหนดเอง” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Micro Frontends Architecture with Module Federation นี้ได้ไหม

ได้ บทเรียน Micro Frontends Architecture with Module Federation ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. บัสเหตุการณ์สำหรับการสื่อสารข้ามแอป
  2. การจัดการสถานะร่วม
  3. โซลูชันการสื่อสารแบบกำหนดเอง
  4. การสื่อสารด้วยเหตุการณ์ DOM แบบกำหนดเอง
← กลับไปที่ Micro Frontends Architecture with Module Federation