Consider this Vue 3 plugin that adds a global property. What will be the output when the component uses this.$greet()?
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');
Remember that app.config.globalProperties adds properties accessible via this inside components.
The plugin adds $greet as a global property. Inside the component, this.$greet() calls that function, returning 'Hello from plugin!'.
Which of the following options correctly defines a Vue 3 plugin that adds a global method $log to all components?
The install method receives the app instance as an argument.
Option A correctly defines an object with an install method that receives app and sets app.config.globalProperties.$log. Other options misuse the app instance or context.
Given this plugin code, why does this.$notify() inside a component cause an error?
const notifyPlugin = { install() { app.config.globalProperties.$notify = () => alert('Notify!'); } };
Check the install method signature and how it uses the app instance.
The install method must receive the app instance as a parameter. Without it, app is undefined, so accessing app.config causes an error.
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?
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');
The plugin adds a reactive object as a global property. The component increments its value on mount.
The reactive object countState starts with value 0. The component increments this.$count.value by 1 on mount, so the displayed value is 1.
Choose the correct statement about Vue 3 plugin creation and usage.
Think about how plugins add features and when they must be applied.
Option B is true: plugins add global properties via app.config.globalProperties. Option B is false because plugins can be functions or objects. Option B is false; components must be registered explicitly. Option B is false; plugins must be used before mounting.