You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/guide/web-components.md
+21-10Lines changed: 21 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -165,21 +165,26 @@ The [Provide / Inject API](/guide/component-provide-inject.html#provide-inject)
165
165
166
166
### SFC as Custom Element
167
167
168
-
The official build toolchains have built-in support for importing Vue Single File Components (SFCs) as custom elements. By default, files ending in `*.ce.vue` will be processed as native custom elements when imported (created with `defineCustomElement`):
168
+
`defineCustomElement` also works with Vue Single File Components (SFCs). However, with the default tooling setup, the `<style>` inside the SFCs will still be extracted and merged into a single CSS file during production build. When using an SFC as a custom element, it is often desirable to inject the `<style>` tags into the custom element's shadow root instead.
169
+
170
+
The official SFC toolings support importing SFCs in "custom element mode" (requires `@vitejs/plugin-vue@^1.4.0` or `vue-loader@^16.5.0`). An SFC loaded in custom element mode inlines its `<style>` tags as strings of CSS and exposes them under the component's `styles` option. This will be picked up by `defineCustomElement` and injected into the element's shadow root when instantiated.
171
+
172
+
To opt-in to this mode, simply end your component file name with `.ce.vue`:
When imported as custom elements, `<style>` inside the SFC will be inlined as JavaScript strings and inserted as a native `<style>` tag inside the custom element's shadow root.
If you wish to customize what files should be imported as custom elements (for example treating _all_ SFCs as custom elements), you can pass the `customElement` option to the respective build plugins:
187
+
If you wish to customize what files should be imported in custom element mode (for example treating _all_ SFCs as custom elements), you can pass the `customElement` option to the respective build plugins:
@@ -193,8 +198,12 @@ If the custom elements will be used in an application that is also using Vue, yo
193
198
It is recommended to export the individual element constructors to give your users the flexibility to import them on-demand and register them with desired tag names. You can also export a convenience function to automatically register all elements. Here's an example entry point of a Vue custom element library:
194
199
195
200
```js
196
-
importMyFoofrom'./MyFoo.ce.vue'
197
-
importMyBarfrom'./MyBar.ce.bar'
201
+
import { defineCustomElement } from'vue'
202
+
importFoofrom'./MyFoo.ce.vue'
203
+
importBarfrom'./MyBar.ce.bar'
204
+
205
+
constMyFoo=defineCustomElement(Foo)
206
+
constMyBar=defineCustomElement(Bar)
198
207
199
208
// export individual elements
200
209
export { MyFoo, MyBar }
@@ -205,6 +214,8 @@ export function register() {
205
214
}
206
215
```
207
216
217
+
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.
218
+
208
219
## Web Components vs. Vue Components
209
220
210
221
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