Vue components giao tiếp qua nhiều cơ chế tùy quan hệ và chiều dữ liệu.
- Props (parent → child): dữ liệu đi xuống, one-way
- Emits (child → parent): event đi lên, gọi
emit() - v-model: two-way binding, kết hợp props + emits
- provide/inject: ancestor → descendant, bỏ qua intermediaries
- Pinia (recommended) hoặc Vuex (legacy): global state store cho app-wide state
- Event bus (ít dùng trong Vue 3):
mittlibrary
Lưu ý: tránh emit từ child để trực tiếp modify prop của parent — luôn emit event để parent tự update.
Vue components communicate through multiple mechanisms depending on the relationship and direction of data flow.
- Props (parent → child): data flows down, one-way
- Emits (child → parent): events flow up, call
emit() - v-model: two-way binding via props + emits
- provide/inject: ancestor → descendant, skipping intermediaries
- Pinia (recommended) or Vuex (legacy): global state store for app-wide state
- Event bus (less common in Vue 3):
mittlibrary
Pitfall: never mutate a prop directly from a child — always emit an event and let the parent update.