High Performance
Built on Vue 3 Composition API with reactive updates and intelligent caching
Easily implement dynamic component switching, error handling, and performance optimization
A powerful Vue 3 dynamic component library that makes component switching, async loading, and error handling effortless.
# npm
npm install ew-vue-component
# yarn
yarn add ew-vue-component
# pnpm
pnpm add ew-vue-component
<template>
<div>
<!-- String component (HTML tag) -->
<EwVueComponent is="button" @click="handleClick">
Click me!
</EwVueComponent>
<!-- Vue component -->
<EwVueComponent :is="MyComponent" :title="title" />
<!-- Async component -->
<EwVueComponent :is="asyncComponent" @error="handleError" />
</div>
</template>
<script setup>
import { ref } from 'vue'
import { EwVueComponent } from 'ew-vue-component'
import MyComponent from './MyComponent.vue'
const title = ref('Hello World')
const asyncComponent = () => import('./AsyncComponent.vue')
const handleClick = () => {
console.log('Button clicked!')
}
const handleError = (error) => {
console.error('Component failed to load:', error)
}
</script>
<template>
<EwVueComponent
:is="currentComponent"
:fallback="FallbackComponent"
:error-component="ErrorComponent"
@error="handleError"
>
<template #default>
<p>Loading...</p>
</template>
</EwVueComponent>
</template>
<script setup>
import { ref, h } from 'vue'
import { EwVueComponent } from 'ew-vue-component'
const currentComponent = ref('UnstableComponent')
const FallbackComponent = {
render() {
return h('div', 'Fallback content when component fails')
}
}
const ErrorComponent = {
props: ['error', 'retry'],
render() {
return h('div', [
h('h3', 'Something went wrong'),
h('p', this.error?.message),
h('button', { onClick: this.retry }, 'Retry')
])
}
}
const handleError = (error) => {
console.error('Component error:', error)
// Send to error monitoring service
}
</script>
<template>
<EwVueComponent
:is="heavyComponent"
:cache="true"
:cache-key="cacheKey"
:cache-ttl="300000"
:plugins="[performancePlugin, logPlugin]"
/>
</template>
<script setup>
import { ref, computed } from 'vue'
import { EwVueComponent } from 'ew-vue-component'
const heavyComponent = () => import('./HeavyComponent.vue')
const componentId = ref('heavy-1')
const cacheKey = computed(() => `heavy-component-${componentId.value}`)
const performancePlugin = {
name: 'performance',
beforeRender(component, props, context) {
performance.mark('component-render-start')
},
afterRender(component, props, context) {
performance.mark('component-render-end')
performance.measure('component-render', 'component-render-start', 'component-render-end')
}
}
const logPlugin = {
name: 'logger',
beforeRender(component, props, context) {
console.log('Rendering component:', component.name || component)
}
}
</script>
EwVueComponent supports multiple component types:
Type | Example | Use Case |
---|---|---|
String | "div" , "button" | HTML tags, simple elements |
Component | MyComponent | Regular Vue components |
Async Function | () => import('./Async.vue') | Code splitting, lazy loading |
Component Object | { render() { ... } } | Dynamic render functions |
<EwVueComponent
:is="component"
@error="handleError"
/>
<EwVueComponent
:is="component"
fallback="div"
/>
<EwVueComponent
:is="component"
:error-component="CustomErrorComponent"
/>
<EwVueComponent
:is="component"
:cache="true"
:cache-key="uniqueKey"
:cache-ttl="300000"
/>
<EwVueComponent
:is="component"
:plugins="[performancePlugin]"
/>
const customPlugin = {
name: 'my-plugin',
beforeRender(component, props, context) {
// Before render logic
},
afterRender(component, props, context) {
// After render logic
},
onError(error, context) {
// Error handling logic
}
}
Build forms with dynamic field types based on configuration:
<EwVueComponent
v-for="field in formFields"
:key="field.id"
:is="field.component"
v-bind="field.props"
@input="updateField(field.id, $event)"
/>
Load remote components dynamically:
<EwVueComponent
:is="() => loadRemoteComponent(appUrl)"
:cache="true"
@error="handleMicrofrontendError"
/>
Render dynamic content based on CMS data:
<EwVueComponent
v-for="block in contentBlocks"
:key="block.id"
:is="componentMap[block.type]"
v-bind="block.props"
/>
Switch between component variants:
<EwVueComponent
:is="experimentVariant"
:plugins="[analyticsPlugin]"
/>
<template>
<div>
<ComponentA v-if="type === 'a'" v-bind="props" />
<ComponentB v-else-if="type === 'b'" v-bind="props" />
<ComponentC v-else-if="type === 'c'" v-bind="props" />
<!-- Manual error handling for each component -->
</div>
</template>
<template>
<EwVueComponent
:is="componentMap[type]"
v-bind="props"
:fallback="FallbackComponent"
@error="handleError"
/>
</template>
We welcome contributions! Please see our Contributing Guide for details.
# Clone the repository
git clone https://github.com/eveningwater/ew-vue-component.git
# Install dependencies
pnpm install
# Run tests
pnpm test
# Build the library
pnpm build
MIT License - see the LICENSE file for details.
Made with โค๏ธ by the EwVueComponent team