Performance: Plugin installation and usage
This affects page load speed and initial rendering time because plugins add code and sometimes extra DOM elements or styles.
import { createApp, defineAsyncComponent } from 'vue'; const app = createApp(App); // Lazy load plugin component only when needed app.component('LazyFeature', defineAsyncComponent(() => import('heavy-plugin'))); app.mount('#app');
import { createApp } from 'vue'; import HeavyPlugin from 'heavy-plugin'; const app = createApp(App); app.use(HeavyPlugin); app.mount('#app');
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Install large plugin upfront | High (adds many nodes) | Multiple reflows | High paint cost | [X] Bad |
| Lazy load plugin components | Low (only when used) | Single reflow | Low paint cost | [OK] Good |
MyPlugin in a Vue 3 app?app.use() with the plugin.const MyPlugin = {
install(app, options) {
app.config.globalProperties.$greet = () => `Hello, ${options.name}!`;
}
};
const app = Vue.createApp({});
app.use(MyPlugin, { name: 'Alice' });
app.mount('#app');
// In a component template: {{ $greet() }}const app = Vue.createApp({});
app.mount('#app');
app.use(MyPlugin);app.mount() to ensure plugin features are available.MyButton and adds a global method $log to log messages. Which is the correct way to write the plugin's install method?app.component() to register components and app.config.globalProperties to add global methods.Pass the test and this pattern moves unknown