0
0
Vueframework~20 mins

Plugin installation and usage 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 3 plugin usage?

Consider a Vue 3 app where a plugin adds a global property $greet that returns a greeting string. What will the component render?

Vue
import { createApp } from 'vue';

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

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

app.use(greetPlugin);

// Mounting omitted for brevity
A<div>Error: $greet is not a function</div>
B<div>undefined</div>
C<div>[object Object]</div>
D<div>Hello from plugin!</div>
Attempts:
2 left
💡 Hint

Remember that plugins can add global properties accessible in components.

📝 Syntax
intermediate
1:30remaining
Which option correctly installs a Vue 3 plugin?

Given a plugin object myPlugin, which code correctly installs it in a Vue 3 app?

Vue
import { createApp } from 'vue';
import myPlugin from './myPlugin';

const app = createApp({});
Aapp.install(myPlugin);
Bapp.use(myPlugin);
Capp.plugin(myPlugin);
Dapp.addPlugin(myPlugin);
Attempts:
2 left
💡 Hint

Vue 3 apps use a specific method to add plugins.

🔧 Debug
advanced
2:30remaining
Why does this plugin usage cause an error?

Given this plugin and component code, why does accessing this.$foo cause an error?

Vue
const fooPlugin = {
  install(app) {
    app.config.globalProperties.$foo = 'bar';
  }
};

const app = Vue.createApp({
  template: `<div>{{ $foo }}</div>`
});

app.use(fooPlugin);
app.mount('#app');
ABecause the component tries to access $foo directly in template without this context
BBecause $foo is a string, not a function, and template expects a function
CBecause the plugin was installed after app creation, so $foo is undefined in template
DBecause globalProperties must be accessed via a method, not directly
Attempts:
2 left
💡 Hint

Think about how Vue templates access properties on the component instance.

state_output
advanced
2:30remaining
What is the value of count after plugin modifies it?

A plugin adds a reactive property count to the app's globalProperties and increments it. What is the final value of count?

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

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

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

const app = createApp({
  mounted() {
    this.$count.count++;
  }
});

app.use(countPlugin);
app.mount('#app');
A2
B1
C0
Dundefined
Attempts:
2 left
💡 Hint

Consider when the plugin increments and when the component increments the count.

🧠 Conceptual
expert
2:00remaining
Which statement about Vue 3 plugin installation order is true?

Consider multiple plugins installed in a Vue 3 app. Which statement about their installation order and effect is correct?

AAll plugins run simultaneously during app creation, so order does not matter
BPlugins are installed in reverse order of <code>app.use()</code> calls, so first called runs last
CPlugins are installed in the order <code>app.use()</code> is called, and later plugins can override earlier ones
DPlugins must be installed before app creation to take effect
Attempts:
2 left
💡 Hint

Think about how JavaScript executes sequential code.