📖 Scenario: You are building a simple Vue 3 app where you want to use a global property and a global method across components.This helps keep your code clean and reuse common values and functions easily.
🎯 Goal: Create a Vue 3 app that sets a global property called $appName with the value 'My Vue App' and a global method called $greet that returns a greeting string.Use these global features inside a component to display the app name and a greeting message.
📋 What You'll Learn
Create a Vue app using createApp
Add a global property $appName with value 'My Vue App'
Add a global method $greet that returns 'Hello from global method!'
Use these global features inside the component's template
💡 Why This Matters
🌍 Real World
Global properties and methods let you share common data and functions easily in Vue apps, like app name, theme colors, or utility functions.
💼 Career
Understanding global properties and methods is important for building scalable Vue applications and working on team projects where shared data and functions reduce repetition.
Progress0 / 4 steps
1
Create the Vue app and component
Create a Vue app using createApp and define a component called App with a template that contains a <div> with the text Welcome to Vue!.
Vue
Hint
Use createApp(App) to create the app and app.mount('#app') to mount it.
2
Add a global property $appName
Add a global property called $appName to the Vue app instance and set its value to 'My Vue App'.
Vue
Hint
Use app.config.globalProperties to add global properties.
3
Add a global method $greet
Add a global method called $greet to the Vue app instance that returns the string 'Hello from global method!'.
Vue
Hint
Assign a function to app.config.globalProperties.$greet that returns the greeting string.
4
Use global property and method in the component template
Update the App component template to display the global property $appName inside an <h1> and call the global method $greet() inside a <p>.
Vue
Hint
Use {{ $appName }} and {{ $greet() }} inside the template to access global property and method.
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.