|
1 | | -# Compatible with Vue |
| 1 | +# Vue compatibility |
2 | 2 |
|
3 | | -You can write Vue components directly in the Markdown file, and it will be parsed. You can use this feature to write vue demo and documentation together. |
| 3 | +Docsify allows Vue [v2.x](https://vuejs.org) and [v3.x](https://v3.vuejs.org) components to be added directly to you Markdown files. These components can greatly simplify working with data and adding reactivity to your content. |
4 | 4 |
|
5 | | -## Basic usage |
| 5 | +To get started, load either the production or development version of Vue in your `index.html`: |
6 | 6 |
|
7 | | -Load the Vue in `./index.html`. |
| 7 | +#### Vue 2.x |
8 | 8 |
|
9 | 9 | ```html |
10 | | -<script src="//cdn.jsdelivr.net/npm/vue"></script> |
11 | | -<script src="//cdn.jsdelivr.net/npm/docsify"></script> |
| 10 | +<!-- Production --> |
| 11 | +<script src="//cdn.jsdelivr.net/npm/vue@2/dist/vue.min.js"></script> |
12 | 12 |
|
13 | | -<!-- Or use the compressed files --> |
14 | | -<script src="//cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script> |
15 | | -<script src="//cdn.jsdelivr.net/npm/docsify/lib/docsify.min.js"></script> |
| 13 | +<!-- Development (debugging and Vue.js devtools support) --> |
| 14 | +<script src="//cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> |
16 | 15 | ``` |
17 | 16 |
|
18 | | -Then you can immediately write Vue code at Markdown file. `new Vue({ el: '#main' })` script is executed by default to create instance. |
| 17 | +#### Vue 3.x |
19 | 18 |
|
20 | | -*README.md* |
| 19 | +```html |
| 20 | +<!-- Production --> |
| 21 | +<script src="//cdn.jsdelivr.net/npm/vue@3/dist/vue.global.prod.js"></script> |
| 22 | + |
| 23 | +<!-- Development (debugging and Vue.js devtools support) --> |
| 24 | +<script src="//cdn.jsdelivr.net/npm/vue@3/dist/vue.global.js"></script> |
| 25 | +``` |
21 | 26 |
|
22 | | -````markdown |
23 | | -# Vue guide |
| 27 | +## Basic rendering |
24 | 28 |
|
25 | | -`v-for` usage. |
| 29 | +Docsify will automatically render basic Vue content that does not require `data`, `methods`, or other instance features. |
26 | 30 |
|
27 | | -```html |
| 31 | +```markdown |
28 | 32 | <ul> |
29 | | - <li v-for="i in 10">{{ i }}</li> |
| 33 | + <li v-for="i in 3">{{ i }}</li> |
30 | 34 | </ul> |
31 | 35 | ``` |
32 | 36 |
|
| 37 | +The HTML above will render the following: |
| 38 | + |
33 | 39 | <ul> |
34 | | - <li v-for="i in 10">{{ i }}</li> |
| 40 | + <li v-for="i in 3">{{ i }}</li> |
35 | 41 | </ul> |
36 | | -```` |
37 | 42 |
|
38 | | -You can manually initialize a Vue instance. |
| 43 | +## Advanced usage |
| 44 | + |
| 45 | +Vue components and templates that require `data`, `methods`, computed properties, lifecycle hooks, etc. require manually creating a new `Vue()` instance within a `<script>` tag in your markdown. |
39 | 46 |
|
40 | | -*README.md* |
| 47 | +<!-- prettier-ignore-start --> |
41 | 48 |
|
42 | 49 | ```markdown |
43 | | -# Vue demo |
| 50 | +<div id="example-1"> |
| 51 | + <p>{{ message }}</p> |
44 | 52 |
|
45 | | -<div id="main">hello {{ msg }}</div> |
| 53 | + <button v-on:click="hello">Say Hello</button> |
46 | 54 |
|
| 55 | + <button v-on:click="counter -= 1">-</button> |
| 56 | + {{ counter }} |
| 57 | + <button v-on:click="counter += 1">+</button> |
| 58 | +</div> |
| 59 | +``` |
| 60 | + |
| 61 | +<!-- prettier-ignore-end --> |
| 62 | + |
| 63 | +#### Vue 2.x |
| 64 | + |
| 65 | +```markdown |
47 | 66 | <script> |
48 | 67 | new Vue({ |
49 | | - el: '#main', |
50 | | - data: { msg: 'Vue' } |
51 | | - }) |
| 68 | + el: "#example-1", |
| 69 | + data: function() { |
| 70 | + return { |
| 71 | + counter: 0, |
| 72 | + message: "Hello, World!" |
| 73 | + }; |
| 74 | + }, |
| 75 | + methods: { |
| 76 | + hello: function() { |
| 77 | + alert(this.message); |
| 78 | + } |
| 79 | + } |
| 80 | + }); |
52 | 81 | </script> |
53 | 82 | ``` |
54 | 83 |
|
55 | | -!> In a Markdown file, only the script within the first script tag is executed. |
| 84 | +#### Vue 3.x |
56 | 85 |
|
57 | | -## Combine Vuep to write playground |
| 86 | +```markdown |
| 87 | +<script> |
| 88 | + Vue.createApp({ |
| 89 | + data: function() { |
| 90 | + return { |
| 91 | + counter: 0, |
| 92 | + message: "Hello, World!" |
| 93 | + }; |
| 94 | + }, |
| 95 | + methods: { |
| 96 | + hello: function() { |
| 97 | + alert(this.message); |
| 98 | + } |
| 99 | + } |
| 100 | + }).mount("#example-1"); |
| 101 | +</script> |
| 102 | +``` |
58 | 103 |
|
59 | | -[Vuep](https://github.com/QingWei-Li/vuep) is a component for rendering Vue components with live editor and preview. Supports Vue component spec and JSX. |
| 104 | +The HTML & JavaScript above will render the following: |
60 | 105 |
|
61 | | -*index.html* |
| 106 | +<!-- prettier-ignore-start --> |
62 | 107 |
|
63 | | -```html |
64 | | -<!-- Inject CSS file --> |
65 | | -<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/vuep/dist/vuep.css"> |
66 | | - |
67 | | -<!-- Inject JavaScript file --> |
68 | | -<script src="//cdn.jsdelivr.net/npm/vue"></script> |
69 | | -<script src="//cdn.jsdelivr.net/npm/vuep"></script> |
70 | | -<script src="//cdn.jsdelivr.net/npm/docsify"></script> |
71 | | - |
72 | | -<!-- or use the compressed files --> |
73 | | -<script src="//cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script> |
74 | | -<script src="//cdn.jsdelivr.net/npm/vuep/dist/vuep.min.js"></script> |
75 | | -<script src="//cdn.jsdelivr.net/npm/docsify/lib/docsify.min.js"></script> |
76 | | -``` |
| 108 | +<div id="example-1"> |
| 109 | + <p>{{ message }}</p> |
77 | 110 |
|
78 | | -*README.md* |
79 | | -```markdown |
80 | | -# Vuep |
| 111 | + <button v-on:click="hello">Say Hello</button> |
81 | 112 |
|
82 | | -<vuep template="#example"></vuep> |
| 113 | + <button v-on:click="counter -= 1">-</button> |
| 114 | + {{ counter }} |
| 115 | + <button v-on:click="counter += 1">+</button> |
| 116 | +</div> |
83 | 117 |
|
84 | | -<script v-pre type="text/x-template" id="example"> |
85 | | - <template> |
86 | | - <div>Hello, {{ name }}!</div> |
87 | | - </template> |
| 118 | +<!-- prettier-ignore-end --> |
88 | 119 |
|
89 | | - <script> |
90 | | - module.exports = { |
91 | | - data: function () { |
92 | | - return { name: 'Vue' } |
| 120 | +!> Only the first `<script>` tag in a markdown file is executed. If you are working with multiple Vue components, all `Vue` instances must be created within this tag. |
| 121 | + |
| 122 | +<script> |
| 123 | + new Vue({ |
| 124 | + el: "#example-1", |
| 125 | + data: function() { |
| 126 | + return { |
| 127 | + counter: 0, |
| 128 | + message: "Hello, World!" |
| 129 | + }; |
| 130 | + }, |
| 131 | + methods: { |
| 132 | + hello: function() { |
| 133 | + alert(this.message); |
93 | 134 | } |
94 | 135 | } |
95 | | - </script> |
| 136 | + }); |
96 | 137 | </script> |
97 | | -``` |
98 | | - |
99 | | -?> Example Refer to the [Vuep documentation](https://qingwei-li.github.io/vuep/). |
|
0 commit comments