bash
npx degit userpmt/vue-ts-template-with-class-components my-vue-webcomponents-app
cd my-vue-webcomponents-app
npm install
npm run dev
Wait, better: I'll provide a complete, copy-paste ready project structure. Create a new folder, add these files, then npm install && npm run dev.
Project Structure
code
my-vue-webcomponents-app/
├── package.json
├── vite.config.ts
├── tsconfig.json
├── tsconfig.node.json
├── index.html
├── src/
│ ├── main.ts
│ ├── App.vue
│ └── components/
│ └── MyCounter.ts
package.json
json
{
"name": "vue3-webcomponents-decorators",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vue-tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"vue": "^3.4.21",
"vue-class-component": "^8.0.0-rc.11"
},
"devDependencies": {
"@tsconfig/node20": "^20.1.2",
"@types/node": "^20.11.19",
"@vitejs/plugin-vue": "^5.0.4",
"typescript": "~5.4.0",
"vite": "^5.2.0",
"vue-tsc": "^2.0.11"
}
}
vite.config.ts
ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig({
plugins: [vue()],
define: {
global: 'globalThis',
},
})
tsconfig.json
json
{
"extends": "@tsconfig/node20/tsconfig.json",
"compilerOptions": {
"composite": true,
"tsBuildInfoFile": "node_modules/.tmp/tsconfig.app.tsbuildinfo",
"target": "ES2020",
"useDefineForClassFields": false,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,
"experimentalDecorators": true,
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
},
"types": ["vite/client"]
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
"exclude": ["node_modules"]
}
tsconfig.node.json
json
{
"extends": "@tsconfig/node20/tsconfig.json",
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
}
index.html
html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vue 3 + Web Components + TS Decorators</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
src/main.ts
ts
import { createApp, defineCustomElement } from 'vue'
import App from './App.vue'
import { MyCounter } from './components/MyCounter'
// Define the Web Component globally
const MyCounterElement = defineCustomElement(MyCounter)
customElements.define('my-counter', MyCounterElement)
const app = createApp(App)
// Configure Vue to recognize custom elements
app.config.isCustomElement = (tag) => tag === 'my-counter'
app.mount('#app')
src/App.vue
vue
<template>
<div id="app">
<h1>Vue 3 + Web Components + TypeScript Decorators</h1>
<p>Below is a Vue-powered Web Component (<my-counter>) built with class-based component and decorators:</p>
<my-counter></my-counter>
<my-counter start="5"></my-counter>
<my-counter start="10" color="red"></my-counter>
</div>
</template>
<script setup lang="ts">
// No script needed, just demo
</script>
<style scoped>
#app {
text-align: center;
padding: 2rem;
}
h1 {
color: #41b883;
}
</style>
src/components/MyCounter.ts
ts
import { defineComponent, Prop, ref, watch } from 'vue'
import { Component, Vue } from 'vue-class-component'
// Class-based Vue component with TypeScript decorators
@Component({
// Inline template using Vue 3 template syntax
template: `
<div class="counter" :style="{ color: color }" @click="decrement">
<button @click.stop="decrement">-</button>
{{ displayCount }}
<button @click.stop="increment">+</button>
</div>
`,
// CSS scoped to shadow DOM automatically in custom elements
styles: [
`
.counter {
display: inline-flex;
align-items: center;
gap: 1rem;
padding: 1rem 2rem;
border: 2px solid #41b883;
border-radius: 8px;
font-size: 1.5rem;
font-weight: bold;
cursor: pointer;
user-select: none;
transition: all 0.2s;
}
.counter:hover {
transform: scale(1.05);
box-shadow: 0 4px 12px rgba(65, 184, 131, 0.3);
}
button {
padding: 0.5rem 1rem;
font-size: 1rem;
background: #41b883;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background: #42d592;
}
`
]
})
export class MyCounter extends Vue {
// Props with @Prop decorator
@Prop({ type: Number, default: 0 }) start!: number
@Prop({ type: String, default: 'blue' }) color!: string
// Reactive data
count = ref(0)
// Lifecycle: watch for prop changes
mounted() {
this.count.value = this.start
}
// Computed for display
get displayCount() {
return this.count.value
}
// Methods
increment() {
this.count.value++
}
decrement() {
this.count.value--
}
}
export default MyCounter
How to Run
- Create the folder structure and copy the files.
- Run
npm install
- Run
npm run dev
- Open http://localhost:5173
Features Demonstrated
- Vue 3 with Composition API (App.vue) and Class Components (
MyCounter.ts).
- TypeScript Decorators:
@Component, @Prop from vue-class-component.
- Web Components:
MyCounter converted to <my-counter> using defineCustomElement. Fully encapsulated with Shadow DOM and scoped styles.
- Props: Pass
start and color props to instances.
- Interactivity: Click buttons to increment/decrement, click counter to decrement.
- Vue Integration: Used seamlessly inside Vue app, with
app.config.isCustomElement.
- TypeScript: Full TS support with decorators enabled.
The Web Component works standalone too: <my-counter start="42" color="purple"></my-counter> anywhere in HTML!
Build with npm run build for production.