Skip to content

Commit d0feec9

Browse files
authored
add guide to typecheck web components written with vue vuejs#2314 (vuejs#2377)
1 parent 6a0a845 commit d0feec9

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/guide/extras/web-components.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,29 @@ export function register() {
224224

225225
If you have many components, you can also leverage build tool features such as Vite's [glob import](https://vitejs.dev/guide/features.html#glob-import) or webpack's [`require.context`](https://webpack.js.org/guides/dependency-management/#requirecontext) to load all components from a directory.
226226

227+
### Web Components and Typescript {#web-components-and-typescript}
228+
229+
If you are developing an application or a library, you may want to [type check](/guide/scaling-up/tooling.html#typescript) your Vue components, including those that are defined as custom elements.
230+
231+
Custom elements are registered globally using native APIs, so by default they won't have type inference when used in Vue templates. To provide type support for Vue components registered as custom elements, we can register global component typings using the the [`GlobalComponents` interface](https://github.com/vuejs/language-tools/blob/master/packages/vscode-vue/README.md#usage) in Vue templates and/or in [JSX](https://www.typescriptlang.org/docs/handbook/jsx.html#intrinsic-elements):
232+
233+
```typescript
234+
import { defineCustomElement } from 'vue'
235+
236+
// vue SFC
237+
import CounterSFC from './src/components/counter.ce.vue'
238+
239+
// turn component into web components
240+
export const Counter = defineCustomElement(CounterSFC)
241+
242+
// register global typings
243+
declare module 'vue' {
244+
export interface GlobalComponents {
245+
'Counter': typeof Counter,
246+
}
247+
}
248+
```
249+
227250
## Web Components vs. Vue Components {#web-components-vs-vue-components}
228251

229252
Some developers believe that framework-proprietary component models should be avoided, and that exclusively using Custom Elements makes an application "future-proof". Here we will try to explain why we believe that this is an overly simplistic take on the problem.

0 commit comments

Comments
 (0)