0
0
Vueframework~20 mins

Global properties and methods in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Global Properties Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will be displayed by this Vue component using a global property?

Consider a Vue 3 app where a global property $appName is set to 'MyVueApp'. What will this component render?

Vue
import { defineComponent } from 'vue';

export default defineComponent({
  template: `<div>{{ $appName }}</div>`
});
A<div>undefined</div>
B<div>$appName</div>
C<div></div>
D<div>MyVueApp</div>
Attempts:
2 left
💡 Hint

Global properties are accessible in templates via this context.

📝 Syntax
intermediate
2:00remaining
Which option correctly registers a global method in Vue 3?

Choose the correct way to add a global method $greet that returns 'Hello' in a Vue 3 app.

Aapp.methods.$greet = () => 'Hello';
Bapp.globalMethods.$greet = () => 'Hello';
Capp.config.globalProperties.$greet = () => 'Hello';
Dapp.globalProperties.methods.$greet = () => 'Hello';
Attempts:
2 left
💡 Hint

Global methods are added to app.config.globalProperties.

🔧 Debug
advanced
2:30remaining
Why does this global property not appear in the component?

Given this setup, why does {{ $version }} show nothing in the component?

const app = Vue.createApp({});
app.config.globalProperties.version = '1.0';
app.mount('#app');
Component template: 
{{ $version }}
AGlobal property must start with a $ sign, so it should be <code>$version</code> in config.
BThe property is set as 'version' but accessed as '$version', causing mismatch.
CGlobal properties are not reactive and cannot be used in templates.
DGlobal properties must be defined before app creation.
Attempts:
2 left
💡 Hint

Check the naming consistency between property definition and usage.

state_output
advanced
2:30remaining
What is the output when a global method modifies a reactive state?

Given this Vue 3 app code, what will be the displayed count after clicking the button once?

const app = Vue.createApp({
  data() { return { count: 0 }; },
  methods: {
    increment() { this.count++; }
  }
});
app.config.globalProperties.$incrementGlobal = function() { this.count += 2; };
app.mount('#app');
Component template:
<button @click="$incrementGlobal()">Add 2</button>
<div>Count: {{ count }}</div>
ACount: 2
BCount: NaN
CCount: 0
DCount: 1
Attempts:
2 left
💡 Hint

Global methods use the component instance as this.

🧠 Conceptual
expert
3:00remaining
Which statement about Vue 3 global properties is TRUE?

Choose the correct statement about global properties and methods in Vue 3.

AGlobal properties can be accessed in components via <code>this</code> and must start with a $ prefix by convention.
BGlobal properties are reactive and automatically update all components when changed.
CGlobal properties must be declared inside each component's <code>data()</code> to be accessible.
DGlobal properties replace Vuex store and are recommended for all state management.
Attempts:
2 left
💡 Hint

Think about how global properties are accessed and naming conventions.