Skip to content

Commit 10d3b62

Browse files
wip(docs): add Vue2 basic example
1 parent c185e35 commit 10d3b62

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

docs/examples.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<!--toc-->
22
* [Examples](#examples)
3+
* [Vue2 basic example](#vue2-basic-example)
34
* [A more complete API usage example](#a-more-complete-api-usage-example)
45
* [Load a Vue component from a string](#load-a-vue-component-from-a-string)
56
* [Using another template language (pug)](#using-another-template-language-pug)
@@ -25,6 +26,53 @@ node -e "require('express')().use(require('express').static(__dirname, {index:'i
2526
```
2627

2728

29+
## Vue2 basic example
30+
31+
**note:** Vue2 do not have the `Vue.defineAsyncComponent()` function. Here we mount the app when the main component is ready.
32+
33+
<!--example:source:vue2_basic_example-->
34+
```html
35+
<!DOCTYPE html>
36+
<html>
37+
<body>
38+
<div id="app"></div>
39+
<script src="https://unpkg.com/vue"></script>
40+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue2-sfc-loader.js"></script>
41+
<script>
42+
43+
/* <!-- */
44+
const mainComponent = `
45+
<template>
46+
<span>Hello World !</span>
47+
</template>
48+
`;
49+
/* --> */
50+
51+
const options = {
52+
moduleCache: {
53+
vue: Vue,
54+
},
55+
getFile(url) {
56+
57+
if ( url === './main.vue' )
58+
return Promise.resolve(mainComponent);
59+
},
60+
addStyle() {},
61+
}
62+
63+
const { loadModule, vueVersion } = window['vue2-sfc-loader'];
64+
loadModule('./main.vue', options)
65+
.then(component => new Vue(component).$mount('#app'));
66+
</script>
67+
</body>
68+
</html>
69+
```
70+
<!--example:target:vue2_basic_example-->
71+
[open in JSBin ▶](http://jsbin.com/?html,output&html=%3C!DOCTYPE+html%3E%0A%3Chtml%3E%0A%3Cbody%3E%0A++%3Cdiv+id%3D%22app%22%3E%3C%2Fdiv%3E%0A++%3Cscript+src%3D%22https%3A%2F%2Funpkg.com%2Fvue%22%3E%3C%2Fscript%3E%0A++%3Cscript+src%3D%22https%3A%2F%2Fcdn.jsdelivr.net%2Fnpm%2Fvue3-sfc-loader%400.4.5%2Fdist%2Fvue2-sfc-loader.js%22%3E%3C%2Fscript%3E%0A++%3Cscript%3E%0A%0A++++%2F*+%3C!--+*%2F%0A++++const+mainComponent+%3D+%60%0A++++++%3Ctemplate%3E%0A++++++++%3Cspan%3EHello+World+!%3C%2Fspan%3E%0A++++++%3C%2Ftemplate%3E%0A++++%60%3B%0A++++%2F*+--%3E+*%2F%0A%0A++++const+options+%3D+%7B%0A++++++moduleCache%3A+%7B%0A++++++++vue%3A+Vue%2C%0A++++++%7D%2C%0A++++++getFile(url)+%7B%0A%0A++++++++if+(+url+%3D%3D%3D+'.%2Fmain.vue'+)%0A++++++++++return+Promise.resolve(mainComponent)%3B%0A++++++%7D%2C%0A++++++addStyle()+%7B%7D%2C%0A++++%7D%0A%0A++++const+%7B+loadModule%2C+vueVersion+%7D+%3D+window%5B'vue2-sfc-loader'%5D%3B%0A++++loadModule('.%2Fmain.vue'%2C+options)%0A++++.then(component+%3D%3E+new+Vue(component).%24mount('%23app'))%3B%0A++%3C%2Fscript%3E%0A%3C%2Fbody%3E%0A%3C%2Fhtml%3E%0A)<!--/example:target:vue2_basic_example-->
72+
[:top:](#readme)
73+
74+
75+
2876
## A more complete API usage example
2977

3078
<!--example:source:complete_api-->

0 commit comments

Comments
 (0)