跳到正文

兼容 Vue ​

Docsify 允许直接在 Markdown 页面中添加 Vue.js 内容。这可以大大简化数据处理,并为你的站点添加响应式能力。

Vue 的模板语法可以用来为页面添加动态内容。当使用数据、计算属性、方法和生命周期钩子时,Vue 内容会变得更加有趣。这些选项可以作为全局选项指定,也可以在 DOM 挂载点和组件中指定。

设置 ​

首先,将 Vue.js 添加到你的 index.html 文件中。正式站点请选择生产版本;开发版本则提供有用的控制台警告和 Vue.js devtools 支持。

html
<!-- Production -->
<script src="//cdn.jsdelivr.net/npm/vue@3/dist/vue.global.prod.js"></script>

<!-- Development -->
<script src="//cdn.jsdelivr.net/npm/vue@3/dist/vue.global.js"></script>

模板语法 ​

Vue 模板语法提供了一些有用的功能,例如支持 JavaScript 表达式,以及用于循环和条件渲染的 Vue 指令。

markdown
<!-- 在 docsify 中隐藏,在其他地方(例如 GitHub)显示 -->
<p v-if="false">Text for GitHub</p>

<!-- 序列内容(即循环)-->
<ul>
  <li v-for="i in 3">Item {{ i }}</li>
</ul>

<!-- JavaScript 表达式 -->
<p>2 + 2 = {{ 2 + 2 }}</p>

Text for GitHub

  • Item {{ i }}

2 + 2 = {{ 2 + 2 }}

在 GitHub 上查看输出

代码块 ​

默认情况下,Docsify 会忽略代码块中的 Vue 模板语法:

markdown
```
{{ message }}
```

如果要处理代码块中的 Vue 模板语法,请用带有 v-template 属性的元素包裹该代码块:

markdown
<div v-template>

```
{{ message }}
```

</div>

数据 ​

js
{
  data() {
    return {
      message: 'Hello, World!'
    };
  }
}
markdown
<!-- 在 docsify 中显示 message,在其他地方(例如 GitHub)显示 "{{ message }}" -->
{{ message }}

<!-- 在 docsify 中显示 message,在其他地方(例如 GitHub)隐藏 -->
<p v-text="message"></p>

{{ message }}

在 GitHub 上查看输出

计算属性 ​

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 }}!

方法 ​

js
{
  data() {
    return {
      message: 'Hello, World!'
    };
  },
  methods: {
    hello() {
      alert(this.message);
    }
  },
}
markdown
<button @click="hello">Say Hello</button>

生命周期钩子 ​

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 响应:
// [
//   { 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
<div style="display: flex;">
  <figure style="flex: 1;">
    <img v-for="image in images" :src="image.url" :title="image.title">
    <figcaption>{{ image.title }}</figcaption>
  </figure>
</div>
{{ image.title }}

全局选项 ​

使用 vueGlobalOptions 指定全局 Vue 选项,这些选项用于没有通过 vueMounts、vueComponents 或 Markdown 脚本显式挂载的 Vue 内容。对全局 data 的更改会持续保留,并反映在所有使用全局引用的地方。

js
window.$docsify = {
  vueGlobalOptions: {
    data() {
      return {
        count: 0,
      };
    },
  },
};
markdown
<p>
  <button @click="count += 1">+</button>
  {{ count }}
  <button @click="count -= 1">-</button>
</p>

{{ count }}

请注意渲染多个全局计数器时的行为:

{{ count }}

对其中一个计数器的更改会同时影响两个计数器,这是因为两个实例引用的是同一个全局 count 值。现在,打开另一个页面再返回本节,可以看到对全局数据的更改在页面加载之间被保留了下来。

挂载点 ​

使用 vueMounts 指定要挂载为 Vue 实例的 DOM 元素及其相关选项。挂载元素以 CSS 选择器作为键,以包含 Vue 选项的对象作为值。每次加载新页面时,Docsify 都会挂载主内容区域中第一个匹配的元素。挂载元素的 data 对每个实例都是独立的,用户浏览站点时不会保留。

js
window.$docsify = {
  vueMounts: {
    '#counter': {
      data() {
        return {
          count: 0,
        };
      },
    },
  },
};
markdown
<div id="counter">
  <button @click="count += 1">+</button>
  {{ count }}
  <button @click="count -= 1">-</button>
</div>
{{ count }}

组件 ​

使用 vueComponents 创建并注册全局 Vue 组件。组件以组件名称作为键,以包含 Vue 选项的对象作为值。组件的 data 对每个实例都是独立的,用户浏览站点时不会保留。

js
window.$docsify = {
  vueComponents: {
    'button-counter': {
      template: `
        <button @click="count += 1">
          You clicked me {{ count }} times
        </button>
      `,
      data() {
        return {
          count: 0,
        };
      },
    },
  },
};
markdown
<button-counter></button-counter>
<button-counter></button-counter>

Markdown 脚本 ​

可以在 Markdown 页面中使用 <script> 标签挂载 Vue 内容。

重要

只有 Markdown 文件中的第一个 <script> 标签会被执行。如果你想用脚本标签挂载多个 Vue 实例,所有实例都必须在 Markdown 的第一个脚本标签内挂载。

html
<script>
  Vue.createApp({
    // 选项...
  }).mount('#example');
</script>

技术说明 ​

  • Docsify 在每次加载页面时按以下顺序处理 Vue 内容:
    1. 执行 Markdown 中的 <script>
    2. 注册全局 vueComponents
    3. 挂载 vueMounts
    4. 自动挂载尚未挂载的 vueComponents
    5. 使用 vueGlobalOptions 自动挂载尚未挂载的 Vue 模板语法
  • 自动挂载 Vue 内容时,docsify 会挂载 Markdown 中每个包含模板语法或组件的顶级元素。例如,在下面的 HTML 中,顶级的 <p>、<my-component /> 和 <div> 元素会被挂载。
    html
    <p>{{ foo }}</p>
    <my-component />
    <div>
      <span>{{ bar }}</span>
      <some-other-component />
    </div>
  • Docsify 不会挂载已有的 Vue 实例,也不会挂载包含已有 Vue 实例的元素。
  • Docsify 会在每次加载页面之前,自动销毁/卸载它创建的所有 Vue 实例。

基于 MIT 许可证发布。