Global properties are values or functions added to the Vue app instance that can be accessed in any component without importing them individually.
Click to reveal answer
beginner
How do you add a global method in Vue 3?
Use app.config.globalProperties to add a method, for example: app.config.globalProperties.$myMethod = () => { ... }.
Click to reveal answer
intermediate
How can you access a global property inside a Vue component?
Inside a component, access it with this.$propertyName in Options API or getCurrentInstance().appContext.config.globalProperties.$propertyName in Composition API.
Click to reveal answer
beginner
Why use global properties and methods in Vue?
They help share common utilities or data across components without repeated imports, making code cleaner and easier to maintain.
Click to reveal answer
intermediate
What is a common use case for global methods in Vue?
Common uses include formatting dates, logging, or accessing shared services like API clients from any component easily.
Click to reveal answer
How do you add a global property in Vue 3?
Aapp.config.globalProperties.$property = value
BVue.prototype.$property = value
Capp.global.property = value
DVue.globalProperties.$property = value
✗ Incorrect
In Vue 3, global properties are added using app.config.globalProperties.
Which of these is a correct way to access a global method inside a Vue component using Options API?
AgetCurrentInstance().$myMethod()
Bapp.$myMethod()
Cthis.$myMethod()
DVue.$myMethod()
✗ Incorrect
Inside a component, global methods are accessed with this.$methodName.
Why should you avoid adding too many global properties in Vue?
AGlobal properties are private and cannot be accessed
BIt can make debugging harder and components less reusable
CVue does not support more than 5 global properties
DIt improves performance too much
✗ Incorrect
Too many global properties can clutter the app and make debugging and reuse difficult.
Which Vue API helps you access global properties in Composition API?
AgetCurrentInstance()
BuseGlobal()
CuseApp()
DgetAppInstance()
✗ Incorrect
getCurrentInstance() returns the component instance, from which you can access globalProperties.
Global methods in Vue are typically prefixed with what symbol?
A&
B#
C@
D$
✗ Incorrect
By convention, global properties and methods start with a $ to distinguish them.
Explain how to add and use a global method in a Vue 3 app.
Think about how the app instance config is used.
You got /3 concepts.
Describe benefits and potential downsides of using global properties in Vue.
Consider both convenience and maintainability.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of app.config.globalProperties in Vue?
easy
A. To add properties or methods accessible in all components
B. To register new components globally
C. To configure routing paths
D. To define component-specific data
Solution
Step 1: Understand the role of app.config.globalProperties
This object allows adding properties or methods that every Vue component can access easily.
Step 2: Differentiate from other Vue features
Registering components, routing, and component data are handled differently, not via globalProperties.
Final Answer:
To add properties or methods accessible in all components -> Option A
Quick Check:
Global properties = shared access across components [OK]
Hint: Global properties share code across all components [OK]
Common Mistakes:
Confusing globalProperties with component registration
Thinking it configures routing
Assuming it stores component local data
2. Which of the following is the correct way to add a global method named logMessage in Vue 3?
easy
A. app.globalProperties.logMessage = () => console.log('Hi')
B. app.config.globalProperties.logMessage = function logMessage() { console.log('Hi') }
C. app.config.globalProperties.logMessage = () => console.log('Hi')
D. app.config.globalProperties.$logMessage = () => console.log('Hi')
Solution
Step 1: Recall the correct syntax for global properties
Global properties must be added to app.config.globalProperties with a $ prefix on the property name.
Step 2: Check the options for correct prefix and assignment
app.config.globalProperties.$logMessage = () => console.log('Hi') correctly uses app.config.globalProperties.$logMessage with an arrow function.
Final Answer:
app.config.globalProperties.$logMessage = () => console.log('Hi') -> Option D
Quick Check:
Use app.config.globalProperties.$name for globals [OK]
Hint: Global properties need $ prefix and app.config [OK]
Common Mistakes:
Omitting the $ prefix
Using app.globalProperties instead of app.config.globalProperties
D. Should use arrow function instead of regular function
Solution
Step 1: Check the global property name
The property is named log but used as $log in 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 A
Quick Check:
Global properties need $ prefix to match usage [OK]
Hint: Global property name and usage must both have $ prefix [OK]
Common Mistakes:
Defining without $ but using with $
Thinking function syntax is wrong
Believing global properties can't be used in components
5. You want to create a global property $formatDate that formats a Date object to YYYY-MM-DD. Which is the best way to define and use it in Vue 3?
hard
A. Add a global variable formatDate in main.js and use formatDate(date) in components
B. Create a component method named formatDate and call this.formatDate(date)
C. Define in app.config.globalProperties.$formatDate as a function returning formatted string; call with this.$formatDate(date) in components
D. Use a computed property in each component to format dates
Solution
Step 1: Understand global property usage
Global properties are added to app.config.globalProperties with a $ prefix and accessed via this.$name in components.
Step 2: Evaluate options for best practice
Define in app.config.globalProperties.$formatDate as a function returning formatted string; call with this.$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 C
Quick Check:
Global reusable methods use $ prefix and app.config [OK]
Hint: Use app.config.globalProperties with $ prefix for reusable methods [OK]
Common Mistakes:
Defining methods only inside components losing reuse
Using global variables without Vue context
Not using $ prefix for global properties
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.