|
| 1 | +# Custom Elements API {#custom-elements-api} |
| 2 | + |
| 3 | +## defineCustomElement() {#definecustomelement} |
| 4 | + |
| 5 | +This method accepts the same argument as [`defineComponent`](#definecomponent), but instead returns a native [Custom Element](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements) class constructor. |
| 6 | + |
| 7 | +- **Type** |
| 8 | + |
| 9 | + ```ts |
| 10 | + function defineCustomElement( |
| 11 | + component: |
| 12 | + | (ComponentOptions & CustomElementsOptions) |
| 13 | + | ComponentOptions['setup'], |
| 14 | + options?: CustomElementsOptions |
| 15 | + ): { |
| 16 | + new (props?: object): HTMLElement |
| 17 | + } |
| 18 | + |
| 19 | + interface CustomElementsOptions { |
| 20 | + styles?: string[] |
| 21 | + |
| 22 | + // the following options are 3.5+ |
| 23 | + configureApp?: (app: App) => void |
| 24 | + shadowRoot?: boolean |
| 25 | + nonce?: string |
| 26 | + } |
| 27 | + ``` |
| 28 | + |
| 29 | + > Type is simplified for readability. |
| 30 | +
|
| 31 | +- **Details** |
| 32 | + |
| 33 | + In addition to normal component options, `defineCustomElement()` also supports a number of options that are custom-elements-specific: |
| 34 | + |
| 35 | + - **`styles`**: an array of inlined CSS strings for providing CSS that should be injected into the element's shadow root. |
| 36 | + |
| 37 | + - **`configureApp`** <sup class="vt-badge" data-text="3.5+"/>: a function that can be used to configure the Vue app instance for the custom element. |
| 38 | + |
| 39 | + - **`shadowRoot`** <sup class="vt-badge" data-text="3.5+"/>: `boolean`, defaults to `true`. Set to `false` to render the custom element without a shadow root. This means `<style>` in custom element SFCs will no longer be encapsulated. |
| 40 | + |
| 41 | + - **`nonce`** <sup class="vt-badge" data-text="3.5+"/>: `string`, if provided, will be set as the `nonce` attribute on style tags injected to the shadow root. |
| 42 | + |
| 43 | + Note that instead of being passed as part of the component itself, these options can also be passed via a second argument: |
| 44 | + |
| 45 | + ```js |
| 46 | + import Element from './MyElement.ce.vue' |
| 47 | + |
| 48 | + defineCustomElement(Element, { |
| 49 | + configureApp(app) { |
| 50 | + // ... |
| 51 | + } |
| 52 | + }) |
| 53 | + ``` |
| 54 | + |
| 55 | + The return value is a custom element constructor that can be registered using [`customElements.define()`](https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/define). |
| 56 | + |
| 57 | +- **Example** |
| 58 | + |
| 59 | + ```js |
| 60 | + import { defineCustomElement } from 'vue' |
| 61 | + |
| 62 | + const MyVueElement = defineCustomElement({ |
| 63 | + /* component options */ |
| 64 | + }) |
| 65 | + |
| 66 | + // Register the custom element. |
| 67 | + customElements.define('my-vue-element', MyVueElement) |
| 68 | + ``` |
| 69 | + |
| 70 | +- **See also** |
| 71 | + |
| 72 | + - [Guide - Building Custom Elements with Vue](/guide/extras/web-components#building-custom-elements-with-vue) |
| 73 | + |
| 74 | + - Also note that `defineCustomElement()` requires [special config](/guide/extras/web-components#sfc-as-custom-element) when used with Single-File Components. |
| 75 | + |
| 76 | +## useHost() <sup class="vt-badge" data-text="3.5+"/> {#usehost} |
| 77 | + |
| 78 | +A Composition API helper that returns the host element of the current Vue custom element. |
| 79 | + |
| 80 | +## useShadowRoot() <sup class="vt-badge" data-text="3.5+"/> {#useshadowroot} |
| 81 | + |
| 82 | +A Composition API helper that returns the shadow root of the current Vue custom element. |
| 83 | + |
| 84 | +## this.$host <sup class="vt-badge" data-text="3.5+"/> {#this-host} |
| 85 | + |
| 86 | +An Options API property that exposes the host element of the current Vue custom element. |
0 commit comments