Global properties and methods let you share values or functions across all Vue components easily. This helps avoid repeating code and keeps your app organized.
Global properties and methods in Vue
import { createApp } from 'vue' const app = createApp(App) app.config.globalProperties.$myMethod = () => { // your code here } app.mount('#app')
app.config.globalProperties and accessed in components with this.$myMethod.$ prefix to avoid name conflicts with component data or methods.$formatDate to format dates in all components.app.config.globalProperties.$formatDate = (date) => {
return new Date(date).toLocaleDateString()
}$appName that holds the app name string.app.config.globalProperties.$appName = 'My Vue App'$log to print messages with a prefix.app.config.globalProperties.$log = (msg) => console.log('Log:', msg)
This example shows how to add global properties and methods to a Vue app. The app displays the app name and formatted date using global properties. The button uses a global method to log a message when clicked.
import { createApp } from 'vue' const App = { template: `<div> <h1>{{ $appName }}</h1> <p>Today is: {{ $formatDate(new Date()) }}</p> <button @click="$log('Button clicked!')">Click me</button> </div>` } const app = createApp(App) app.config.globalProperties.$appName = 'My Vue App' app.config.globalProperties.$formatDate = (date) => { return new Date(date).toLocaleDateString() } app.config.globalProperties.$log = (msg) => console.log('Log:', msg) app.mount('#app')
Global properties are accessible inside component templates and methods via this.$propertyName.
Use global properties sparingly to avoid making components too dependent on global state.
For better type safety and IDE support, consider using provide/inject or composables for shared logic in larger apps.
Global properties and methods let you share code across all Vue components easily.
Add them using app.config.globalProperties with a $ prefix.
Use them in templates and scripts with this.$propertyName.
Practice
app.config.globalProperties in Vue?Solution
Step 1: Understand the role of
This object allows adding properties or methods that every Vue component can access easily.app.config.globalPropertiesStep 2: Differentiate from other Vue features
Registering components, routing, and component data are handled differently, not viaglobalProperties.Final Answer:
To add properties or methods accessible in all components -> Option AQuick Check:
Global properties = shared access across components [OK]
- Confusing globalProperties with component registration
- Thinking it configures routing
- Assuming it stores component local data
logMessage in Vue 3?Solution
Step 1: Recall the correct syntax for global properties
Global properties must be added toapp.config.globalPropertieswith a$prefix on the property name.Step 2: Check the options for correct prefix and assignment
app.config.globalProperties.$logMessage = () => console.log('Hi') correctly usesapp.config.globalProperties.$logMessagewith an arrow function.Final Answer:
app.config.globalProperties.$logMessage = () => console.log('Hi') -> Option DQuick Check:
Useapp.config.globalProperties.$namefor globals [OK]
- Omitting the $ prefix
- Using app.globalProperties instead of app.config.globalProperties
- Assigning without a function
app.config.globalProperties.$greet = (name) => `Hello, ${name}!`
What will be the output of this code inside a component method?
console.log(this.$greet('Alice'))Solution
Step 1: Understand the global method definition
The method$greetis defined globally to return a greeting string with the passed name.Step 2: Analyze the component call
Callingthis.$greet('Alice')invokes the global method, returningHello, Alice!.Final Answer:
Hello, Alice! -> Option BQuick Check:
Global method call returns expected string [OK]
- Expecting template literal without interpolation
- Forgetting to use this.$ prefix
- Assuming method is undefined
app.config.globalProperties.log = function(message) {
console.log(message)
}
// Usage in component:
this.$log('Test')Solution
Step 1: Check the global property name
The property is namedlogbut used as$login the component, causing mismatch.Step 2: Understand Vue global property naming convention
Global properties must be defined with a$prefix to match usage in components.Final Answer:
Missing $ prefix in global property name -> Option AQuick Check:
Global properties need $ prefix to match usage [OK]
- Defining without $ but using with $
- Thinking function syntax is wrong
- Believing global properties can't be used in components
$formatDate that formats a Date object to YYYY-MM-DD. Which is the best way to define and use it in Vue 3?Solution
Step 1: Understand global property usage
Global properties are added toapp.config.globalPropertieswith a$prefix and accessed viathis.$namein components.Step 2: Evaluate options for best practice
Define inapp.config.globalProperties.$formatDateas a function returning formatted string; call withthis.$formatDate(date)in components correctly defines the formatter globally and uses it properly in components, promoting code reuse.Final Answer:
Define in app.config.globalProperties.$formatDate as a function returning formatted string; call with this.$formatDate(date) in components -> Option CQuick Check:
Global reusable methods use $ prefix and app.config [OK]
- Defining methods only inside components losing reuse
- Using global variables without Vue context
- Not using $ prefix for global properties
Ready to prove it?
Pass the test and this pattern moves unknown
