Skip to content

Commit cd0a4f8

Browse files
garychouulivz
authored andcommitted
docs: dynamically register a non-ssr-friendly component (#1900)
* Code block to dynamically register a component As someone relatively new to JS + Vue, the existing sample code for dynamic imports didn't tell me how to actually register the component dynamically, which is the likely case for importing a module. The proposed code block does both: it dynamically imports the module and also registers the component. * docs: register non-ssr-friendly components Using dynamic components to register non-ssr-friendly components. * chore: tweaks
1 parent 2f86010 commit cd0a4f8

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

packages/docs/docs/guide/using-vue.md

+29
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,35 @@ export default {
2626
</script>
2727
```
2828

29+
If your module `export default` a Vue component, you can register it dynamically:
30+
31+
```vue
32+
<template>
33+
<component v-if="dynamicComponent" :is="dynamicComponent"></component>
34+
</template>
35+
36+
<script>
37+
export default {
38+
data() {
39+
return {
40+
dynamicComponent: null
41+
}
42+
},
43+
44+
mounted () {
45+
import('./lib-that-access-window-on-import').then(module => {
46+
this.dynamicComponent = module.default
47+
})
48+
}
49+
}
50+
</script>
51+
```
52+
53+
**Also see:**
54+
55+
- [Vue.js > Dynamic Components](https://vuejs.org/v2/guide/components.html#Dynamic-Components)
56+
57+
2958
## Templating
3059

3160
### Interpolation

0 commit comments

Comments
 (0)