Trung BìnhVue.js iconVue.js

`provide` / `inject` là gì? Khi nào dùng?

provide / inject cho phép truyền data qua component tree mà không cần props drilling.

Parent provide, bất kỳ descendant nào có thể inject:

javascript
// Parent
import { provide, ref } from 'vue'
const theme = ref('dark')
provide('theme', theme)

// Child (bất kỳ cấp)
import { inject } from 'vue'
const theme = inject('theme', 'light') // 'light' là default

Dùng khi:

  1. Shared state cho subtree (theme, locale, auth)
  2. Plugin/library cung cấp context

Pitfall: khó debug hơn props vì data flow không explicit — dùng Symbol key để tránh naming collision.

Xem toàn bộ Vue.js cùng filter theo level & chủ đề con.

Mở danh sách Vue.js