Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/plugin-prefer-provide-inject-over-global-properties.md
1# Prefer provide/inject Over Global Properties in Plugins23## Rule45When creating Vue plugins, prefer using `app.provide()` to make plugin functionality available to components instead of attaching properties to `app.config.globalProperties`.67## Why This Matters891. **globalProperties don't work in setup()**: Properties attached to `globalProperties` are only accessible via `this` in Options API. They are NOT available in the Composition API's `setup()` function.10112. **Type safety**: `provide/inject` integrates better with TypeScript and requires less type augmentation boilerplate.12133. **Testability**: Injected dependencies are easier to mock in tests compared to global properties.14154. **Code clarity**: Explicit `inject()` calls make dependencies visible, while global properties can appear "magic".16175. **Scoping**: `provide/inject` follows Vue's component hierarchy, making it easier to provide different values to different parts of your app.1819## Bad Practice2021```typescript22// plugins/i18n.ts23export default {24install(app, options) {25// Attaching to globalProperties - only works with Options API26app.config.globalProperties.$translate = (key: string) => {27return key.split('.').reduce((o, i) => o?.[i], options)28}29}30}3132// In component - requires type augmentation for TypeScript33// Also DOES NOT work in <script setup>34export default {35mounted() {36console.log(this.$translate('greeting.hello'))37}38}39```4041## Good Practice4243```typescript44// plugins/i18n.ts45import type { InjectionKey, App } from 'vue'4647export interface I18nOptions {48[key: string]: string | I18nOptions49}5051export interface I18n {52translate: (key: string) => string53options: I18nOptions54}5556export const i18nKey: InjectionKey<I18n> = Symbol('i18n')5758export default {59install(app: App, options: I18nOptions) {60const translate = (key: string): string => {61return key.split('.').reduce((o, i) => o?.[i], options) as string ?? key62}6364// Use provide for Composition API compatibility65app.provide(i18nKey, { translate, options })66}67}6869// In component - works in setup() and has full type safety70<script setup lang="ts">71import { inject } from 'vue'72import { i18nKey } from '@/plugins/i18n'7374const i18n = inject(i18nKey)75console.log(i18n?.translate('greeting.hello'))76</script>77```7879## Hybrid Approach8081If you must support both APIs (e.g., for backwards compatibility), provide both:8283```typescript84export default {85install(app: App, options: I18nOptions) {86const i18n = {87translate: (key: string) => /* ... */88}8990// For Composition API91app.provide(i18nKey, i18n)9293// For Options API (use sparingly)94app.config.globalProperties.$i18n = i18n95}96}97```9899## TypeScript Type Augmentation (if using globalProperties)100101If you must use globalProperties, you need proper type augmentation:102103```typescript104// types/vue.d.ts105export {}106107declare module 'vue' {108interface ComponentCustomProperties {109$translate: (key: string) => string110}111}112```113114**Important**: The file MUST contain `export {}` or another top-level export/import. Without it, the augmentation will OVERWRITE types instead of augmenting them.115116## References117118- [Vue.js Plugins Documentation](https://vuejs.org/guide/reusability/plugins.html)119- [Vue.js Provide/Inject](https://vuejs.org/guide/components/provide-inject.html)120- [TypeScript with Options API](https://vuejs.org/guide/typescript/options-api.html)121