Using Vue in Markdown

    If you are using or demoing components that are not SSR friendly (for example containing custom directives), you can wrap them inside the built-in <ClientOnly> component:

    Note this does not fix components or libraries that access Browser APIs on import - to use code that assumes a browser environment on import, you need to dynamically import them in proper lifecycle hooks:

    1. <script>
    2. export default {
    3. mounted () {
    4. import('./lib-that-access-window-on-import').then(module => {
    5. // use code
    6. })
    7. }
    8. }
    9. </script>

    If your module export default a Vue component, you can register it dynamically:

    1. <template>
    2. <component v-if="dynamicComponent" :is="dynamicComponent"></component>
    3. </template>
    4. export default {
    5. data() {
    6. return {
    7. dynamicComponent: null
    8. }
    9. },
    10. mounted () {
    11. import('./lib-that-access-window-on-import').then(module => {
    12. this.dynamicComponent = module.default
    13. })
    14. }
    15. }
    16. </script>

    Also see:

    Templating

    Each Markdown file is first compiled into HTML and then passed on as a Vue component to vue-loader. This means you can use Vue-style interpolation in text:

    Input

    Output

    1. 2

    Directives

    Directives also work:

    Input

    1. <span v-for="i in 3">{{ i }} </span>

    Output

    The compiled component does not have any private data but does have access to the . For example:

    Input

    1. {{ $page }}

    Output

    1. {
    2. "path": "/using-vue.html",
    3. "title": "Using Vue in Markdown",
    4. "frontmatter": {}
    5. }

    Input

    1. ::: v-pre
    2. `{{ This will be displayed as-is }}`
    3. :::

    Output

    {{ This will be displayed as-is }}

    Using Components

    Any *.vue files found in .vuepress/components are automatically registered as , async components. For example:

    1. .
    2. └─ components
    3. ├─ demo-1.vue
    4. ├─ OtherComponent.vue
    5. └─ Foo
    6. └─ Bar.vue

    Inside any Markdown file you can then directly use the components (names are inferred from filenames):

    1. <demo-1/>
    2. <OtherComponent/>
    3. <Foo-Bar/>

    Hello this is <demo-1>

    This is another component

    Hello this is <Foo-Bar>

    IMPORTANT

    Make sure a custom component’s name either contains a hyphen or is in PascalCase. Otherwise it will be treated as an inline element and wrapped inside a <p> tag, which will lead to hydration mismatch because <p> does not allow block elements to be placed inside it.

    Using Components In Headers

    You can use Vue components in the headers, but note the difference between the following two ways:

    The HTML wrapped by <code> will be displayed as is, only the HTML that is not wrapped will be parsed by Vue.

    TIP

    VuePress has built-in webpack config for the following pre-processors: sass, scss, less, stylus and pug. All you need to do is installing the corresponding dependencies. For example, to enable sass, install the following in your project:

    1. yarn add -D sass-loader node-sass

    Now you can use the following in Markdown and theme components:

    1. <style lang="sass">
    2. .title
    3. font-size: 20px
    4. </style>

    Using <template lang="pug"> requires installing pug and pug-plain-loader:

    1. yarn add -D pug pug-plain-loader

    TIP

    If you are a Stylus user, you don’t need to install stylus and stylus-loader in your project because VuePress uses Stylus internally.

    For pre-processors that do not have built-in webpack config support, you will need to extend the internal webpack config and install the necessary dependencies.

    Script & Style Hoisting

    Sometimes you may need to apply some JavaScript or CSS only to the current page. In those cases, you can directly write root-level <script> or <style> blocks in the Markdown file, and they will be hoisted out of the compiled HTML and used as the <script> and <style> blocks for the resulting Vue single-file component.

    It(Using Vue in Markdown - 图6) is used to specify that this is an external link. In VuePress, this component has been followed by every external link.

    ClientOnly stable

    See .

    • Props:

      • pageKey - string, page's hash key, defaults to current page’s key.
      • slotKey - string, key of . Defaults to default slot.
    • Usage

    Specify a specific slot for a specific page (.md) for rendering. This will be useful when you use or Writing a theme

    1. <Content/>

    Also see:

    Badge beta default theme

    • Props:

      • text - string
      • type - string, optional value: "tip"|"warning"|"error", defaults to "tip".
      • vertical - string, optional value: "top"|"middle", defaults to .
    • Usage:

    Also see: