0
0
Vueframework~10 mins

Global properties and methods in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Global properties and methods
Create Vue App
Define Global Properties
Mount App
Access Global Properties in Components
Use Global Methods
Component Renders with Global Access
This flow shows how to add global properties and methods to a Vue app, then use them inside components.
Execution Sample
Vue
import { createApp } from 'vue'
const app = createApp({})
app.config.globalProperties.$greet = () => 'Hello!'
app.mount('#app')
This code creates a Vue app, adds a global method $greet, and mounts the app.
Execution Table
StepActionGlobal Properties StateComponent AccessOutput
1Create Vue app{}N/ANo output
2Add global method $greet{$greet: function}N/ANo output
3Mount app{$greet: function}Components can access $greetNo output
4Component calls $greet(){$greet: function}Access this.$greet()Returns 'Hello!'
5Component renders greeting{$greet: function}Used in templateDisplays 'Hello!'
💡 App is mounted and components can use global properties and methods
Variable Tracker
VariableStartAfter Step 2After Step 3Final
app.config.globalProperties{}{$greet: function}{$greet: function}{$greet: function}
Key Moments - 2 Insights
Why can't we access global properties before mounting the app?
Global properties are added before mounting, but components only access them after mounting, as shown in execution_table steps 2 and 3.
How do components access global methods like $greet?
Components access global methods via this.$greet or using setup context, as shown in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of globalProperties after step 2?
AIt contains the mounted app instance
BIt is still empty
CIt contains the $greet function
DIt contains component data
💡 Hint
Check the 'Global Properties State' column at step 2 in the execution_table
At which step do components start accessing the global method $greet?
AStep 2
BStep 3
CStep 1
DStep 5
💡 Hint
Look at the 'Component Access' column in the execution_table
If we add another global property $farewell, when will components be able to use it?
AOnly after mounting the app
BImmediately after adding it
COnly after the component is created
DOnly after the app is unmounted
💡 Hint
Refer to the flow from adding global properties to mounting in concept_flow and execution_table
Concept Snapshot
Vue Global Properties & Methods:
- Add via app.config.globalProperties
- Use $ prefix (e.g., $greet)
- Add before mounting app
- Access in components via this.$greet
- Useful for shared functions or values
Full Transcript
In Vue, you create an app instance first. Then you add global properties or methods to app.config.globalProperties. These are shared across all components. After adding, you mount the app to the DOM. Components can then access these global properties using this.$propertyName. For example, a global method $greet can be called inside any component to return a greeting. This helps share common functions or data easily. The execution table shows each step: creating the app, adding global methods, mounting, and component usage. Remember, components only see global properties after the app is mounted.