0
0
Vueframework~20 mins

Plugin creation basics in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Plugin Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Vue plugin usage?

Consider this Vue 3 plugin that adds a global property. What will be the output when the component uses this.$greet()?

Vue
import { createApp } from 'vue';

const greetPlugin = {
  install(app) {
    app.config.globalProperties.$greet = () => 'Hello from plugin!';
  }
};

const app = createApp({
  template: '<div>{{ greetMessage }}</div>',
  data() {
    return { greetMessage: '' };
  },
  mounted() {
    this.greetMessage = this.$greet();
  }
});

app.use(greetPlugin);
app.mount('#app');
AError: this.$greet is not a function
B'' (empty string)
Cundefined
DHello from plugin!
Attempts:
2 left
💡 Hint

Remember that app.config.globalProperties adds properties accessible via this inside components.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a Vue 3 plugin?

Which of the following options correctly defines a Vue 3 plugin that adds a global method $log to all components?

Aconst logPlugin = { install(app) { app.config.globalProperties.$log = (msg) => console.log(msg); } };
Bconst logPlugin = () => { app.config.globalProperties.$log = (msg) => console.log(msg); };
Cconst logPlugin = { install() { this.$log = (msg) => console.log(msg); } };
Dconst logPlugin = { install(app) { app.$log = (msg) => console.log(msg); } };
Attempts:
2 left
💡 Hint

The install method receives the app instance as an argument.

🔧 Debug
advanced
2:00remaining
Why does this Vue plugin fail to add a global property?

Given this plugin code, why does this.$notify() inside a component cause an error?

Vue
const notifyPlugin = {
  install() {
    app.config.globalProperties.$notify = () => alert('Notify!');
  }
};
AThe install method lacks the app parameter, so app is undefined causing an error.
BGlobal properties must be added to app.globalProperties, not app.config.globalProperties.
CThe plugin must return a function, not an object with install method.
DThe alert function is not allowed inside plugins.
Attempts:
2 left
💡 Hint

Check the install method signature and how it uses the app instance.

state_output
advanced
2:00remaining
What is the value of this.$count.value after plugin usage?

This plugin adds a reactive global property $count. What will this.$count.value be inside a component after mounting?

Vue
import { createApp, reactive } from 'vue';

const countState = reactive({ value: 0 });

const countPlugin = {
  install(app) {
    app.config.globalProperties.$count = countState;
  }
};

const app = createApp({
  mounted() {
    this.$count.value++;
  },
  template: '<div>{{ $count.value }}</div>'
});

app.use(countPlugin);
app.mount('#app');
Aundefined
B0
C1
DError: Cannot read property 'value' of undefined
Attempts:
2 left
💡 Hint

The plugin adds a reactive object as a global property. The component increments its value on mount.

🧠 Conceptual
expert
2:00remaining
Which statement about Vue 3 plugins is TRUE?

Choose the correct statement about Vue 3 plugin creation and usage.

AA plugin must always be an object with an install method; functions are not allowed.
BPlugins can add global properties accessible via <code>this</code> inside components using <code>app.config.globalProperties</code>.
CPlugins automatically register components globally without explicit registration code.
DPlugins can only be used before mounting the app; using them after mounting has no effect.
Attempts:
2 left
💡 Hint

Think about how plugins add features and when they must be applied.