# Vue compatibility Docsify allows Vue content to be added directly to you markdown pages. This can greatly simplify working with data and adding reactivity to your site. To get started, add either Vue [2.x](https://vuejs.org) or [3.x](https://v3.vuejs.org) to your `index.html` file: ```html ``` The URLs above will load a **production** version of Vue which has been optimized for performance. Alternatively, **development** versions of Vue are available that are larger in size but offer helpful console warnings and [Vue.js devtools](https://github.com/vuejs/vue-devtools) support: ```html ``` ## Template syntax Vue [template syntax](https://vuejs.org/v2/guide/syntax.html) is used to create dynamic content. With no additinal configuration, this syntax offers several useful features like support for [JavaScript expressions](https://vuejs.org/v2/guide/syntax.html#Using-JavaScript-Expressions) and Vue [directives](https://vuejs.org/v2/guide/syntax.html#Directives) for loops and conditional rendering. ```markdown

Text for GitHub

2 + 2 = {{ 2 + 2 }}

```

Text for GitHub

2 + 2 = {{ 2 + 2 }}

[View output on GitHub](https://github.com/docsifyjs/docsify/blob/develop/docs/vue.md#template-syntax) Vue content becomes more interesting when [data](#data), [computed properties](#computed-properties), [methods](#methods), and [lifecycle hooks](#lifecycle-hooks) are used. These options can be specified as [global options](#global-options), [instance options](#instance-options), or within [components](#components). ### Data ```js { data() { return { message: 'Hello, World!' }; } } ``` ```markdown {{ message }}

Text for GitHub

``` {{ message }}

Text for GitHub

[View output on GitHub](https://github.com/docsifyjs/docsify/blob/develop/docs/vue.md#data) ### Computed properties ```js { computed: { timeOfDay() { const date = new Date(); const hours = date.getHours(); if (hours < 12) { return 'morning'; } else if (hours < 18) { return 'afternoon'; } else { return 'evening' } } }, } ``` ```markdown Good {{ timeOfDay }}! ``` Good {{ timeOfDay }}! ### Methods ```js { data() { return { message: 'Hello, World!' }; }, methods: { hello() { alert(this.message); } }, } ```

### Lifecycle Hooks ```js { data() { return { images: null, }; }, created() { fetch('https://api.domain.com/') .then(response => response.json()) .then(data => (this.images = data)) .catch(err => console.log(err)); } } // API response: // [ // { title: 'Image 1', url: 'https://domain.com/1.jpg' }, // { title: 'Image 2', url: 'https://domain.com/2.jpg' }, // { title: 'Image 3', url: 'https://domain.com/3.jpg' }, // ]; ``` ```markdown
{{ image.title }}
```
{{ image.title }}
## Global options Use `vueGlobalOptions` to share Vue options throughout your site. These options will be used when Docsify detects Vue content in the main content area that has not been previously mounted via [instance options](#instance-options), [components](#components), or a [markdown script](#markdown-script). Global `data()` is shared and changes will persist as users navigate the site. ```js window.$docsify = { vueGlobalOptions: { data() { return { count: 0, }; }, }, }; ``` ```markdown

{{ count }}

``` Notice the behavior when multilpe global counters are rendered below: changes made to one counter affect the other because both instances reference the global `count` value.

{{ count }}

{{ count }}

Now, navigate to a new page and return to this section to see how changes made to global data persist. ## Instance options Use `vueOptions` to specify Vue mount elements and their associated options. Mount elements are specified using a [CSS selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors) as the key with an object containing Vue options as their value. Docsify will mount the first matching element in the main content area (`#main, .markdown-section`) each time a new page is loaded. Instance data is not shared and changes will not persist as users navigate the site. ```js window.$docsify = { vueOptions: { '#counter': { data() { return { count: 0, }; }, }, }, }; ``` ```markdown
{{ count }}
``` {{ count }} ## Components Use `vueComponents` to register Vue components using the component name as the key with an object containing Vue options as the value. Components data is not shared and changes will not persist as users navigate the site. ```js window.$docsify = { vueComponents: { 'button-counter': { template: ` `, data() { return { count: 0, }; }, }, }, }; ``` ```markdown ``` ## Markdown script Vue content can mounted using a ` ``` ```html ``` ## Technical Notes - Docsify processes Vue content in the following order: 1. markdown script, 1. `vueOptions` 1. `vueGlobalOptions` - Docsify will not mount an element that is already a Vue instance or contains a Vue instance. - Docsify will automatically destroy/unmount all Vue instances it creates. - When processing `vueGlobalOptions`, docsify parses each root element of the main content area and mounts the element if Vue content is detected. Docsify does not parse each individual node within the main content area. - When processing `vueGlobalOptions`, docsify detects the full `v-` attribute syntax (e.g `v-bind:href` or `v-on:click`) but not the [shorthand](https://vuejs.org/v2/guide/syntax.html#Shorthands) syntax (e.g. `:href`or `@click`).