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-global-properties-sparingly.md
1# Use Global Properties Sparingly in Plugins23## Rule45When using `app.config.globalProperties` in Vue plugins, use them sparingly and with clear naming conventions. Excessive global properties lead to confusion, naming conflicts, and debugging difficulties.67## Why This Matters891. **Implicit dependencies**: Global properties make component dependencies invisible, making code harder to understand and maintain.10112. **Naming collisions**: Multiple plugins may try to use the same property name (e.g., `$http`, `$api`), causing silent overwrites.12133. **Debugging difficulty**: When issues arise, tracing back to which plugin provides a global property is challenging.14154. **IDE limitations**: Global properties may not have proper autocomplete or type checking without careful configuration.16175. **Testing complexity**: Global state is harder to mock and isolate in unit tests.1819## Bad Practice2021```typescript22// Too many global properties from various plugins23app.config.globalProperties.$http = axios24app.config.globalProperties.$api = apiClient25app.config.globalProperties.$auth = authService26app.config.globalProperties.$translate = i18n.translate27app.config.globalProperties.$format = formatters28app.config.globalProperties.$utils = utilities29app.config.globalProperties.$config = appConfig30app.config.globalProperties.$logger = logger3132// In component - where did all these come from?33export default {34mounted() {35this.$logger.info('Mounted')36const data = await this.$http.get(this.$config.apiUrl)37this.$api.process(this.$utils.transform(data))38}39}40```4142## Good Practice4344```typescript45// Use provide/inject for most functionality46export default {47install(app, options) {48// Provide services via injection49app.provide('api', apiClient)50app.provide('auth', authService)51app.provide('i18n', i18n)5253// Reserve globalProperties for truly global template helpers54// that are used extensively in templates across the app55app.config.globalProperties.$t = i18n.translate // Common convention56}57}5859// In component - explicit dependencies60<script setup>61import { inject } from 'vue'6263const api = inject('api')64const auth = inject('auth')65</script>6667<template>68<!-- $t is acceptable for common template-only usage -->69<h1>{{ $t('welcome') }}</h1>70</template>71```7273## Naming Conventions7475If you do use globalProperties:76771. **Use `$` prefix**: This is the Vue convention and avoids conflicts with component data/methods782. **Use unique prefixes for your library**: e.g., `$myLib_translate` for third-party plugins793. **Document all global properties**: Keep a central registry of what each plugin provides8081```typescript82// Good: namespaced to avoid conflicts83app.config.globalProperties.$myPlugin = {84translate: (key) => /* ... */,85format: (value) => /* ... */86}8788// Usage89{{ $myPlugin.translate('key') }}90```9192## Auditing Global Properties9394You can inspect all global properties for debugging:9596```typescript97console.log(app.config.globalProperties)98```99100## When Global Properties Are Acceptable1011021. **Template-only utilities** used very frequently (like `$t` for translations)1032. **Legacy migration** when transitioning from Vue 21043. **Libraries that need Options API compatibility** (but prefer also providing inject)105106## References107108- [Vue.js Plugins Documentation](https://vuejs.org/guide/reusability/plugins.html)109- [Vue.js Global Properties](https://vuejs.org/api/application.html#app-config-globalproperties)110