Consider a Vue 3 app where a plugin adds a global property $greet that returns a greeting string. What will the component render?
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
Remember that plugins can add global properties accessible in components.
The plugin adds $greet as a global property returning a string. The component calls this.$greet() in computed, so it renders the returned string.
Given a plugin object myPlugin, which code correctly installs it in a Vue 3 app?
import { createApp } from 'vue'; import myPlugin from './myPlugin'; const app = createApp({});
Vue 3 apps use a specific method to add plugins.
The correct method to install a plugin in Vue 3 is app.use(plugin). Other methods do not exist and cause errors.
Given this plugin and component code, why does accessing this.$foo cause an error?
const fooPlugin = { install(app) { app.config.globalProperties.$foo = 'bar'; } }; const app = Vue.createApp({ template: `<div>{{ $foo }}</div>` }); app.use(fooPlugin); app.mount('#app');
Think about how Vue templates access properties on the component instance.
In Vue templates, properties added to globalProperties are accessible via this. Using {{$foo}} works because Vue binds this context. The code is correct, so the error is likely from something else. But here, the plugin adds a string, which is accessible. The error is a trick: the code is valid and should render 'bar'. If an error occurs, it might be from missing import or Vue namespace usage.
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?
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');
Consider when the plugin increments and when the component increments the count.
The plugin increments count once during install (0 to 1). Then the component increments it again on mounted (1 to 2). So final count is 2.
Consider multiple plugins installed in a Vue 3 app. Which statement about their installation order and effect is correct?
Think about how JavaScript executes sequential code.
Vue installs plugins in the order app.use() is called. Later plugins can override properties or methods added by earlier plugins.