了解如何将 Tiptap 集成到你的 Vue 3 项目中,使用本分步指南。或者,你也可以查看我们的 Vue 文本编辑器示例

要求

  • 你的计算机上已安装 Node
  • 你的计算机上已安装 Vue CLI
  • 具备 Vue 的使用经验

创建项目(可选)

如果你已经有一个 Vue 项目,那也没问题,可以跳过此步骤。

在本指南中,我们将从头开始创建一个新的 Vue 项目,命名为 my-tiptap-project。Vue CLI 会自动设置好所需的一切,确保选择 Vue 3 模板。

  1. # 创建项目
  2. vue create my-tiptap-project
  3. # 进入项目目录
  4. cd my-tiptap-project

安装依赖

好了,模板项目已经准备就绪,现在终于可以安装 Tiptap 了!在下面的示例中,你需要安装 @tiptap/vue-3 包、@tiptap/pm(ProseMirror 库)以及 @tiptap/starter-kit,后者包含了大多数常见扩展,可以帮助你快速上手。

  1. npm install @tiptap/vue-3 @tiptap/pm @tiptap/starter-kit

如果你已经完成了上面的步骤,现在可以使用 npm run dev 启动你的项目,并在浏览器中打开 http://localhost:8080。如果你是在现有项目上进行操作,端口号可能会有所不同。

集成 Tiptap

要开始使用 Tiptap,你需要在应用中添加一个新组件。我们将其命名为 Tiptap,并在 components/Tiptap.vue 文件中添加以下示例代码。

这是在 Vue 中快速运行 Tiptap 的最快方法。它提供了一个非常基本的 Tiptap 版本,不带任何按钮。不用担心,稍后你可以添加更多功能。

  1. <template>
  2. <editor-content :editor="editor" />
  3. </template>
  4. <script>
  5. import { Editor, EditorContent } from '@tiptap/vue-3'
  6. import StarterKit from '@tiptap/starter-kit'
  7. export default {
  8. components: {
  9. EditorContent,
  10. },
  11. data() {
  12. return {
  13. editor: null,
  14. }
  15. },
  16. mounted() {
  17. this.editor = new Editor({
  18. content: "<p>I'm running Tiptap with Vue.js. 🎉</p>",
  19. extensions: [StarterKit],
  20. })
  21. },
  22. beforeUnmount() {
  23. this.editor.destroy()
  24. },
  25. }
  26. </script>

或者,你可以使用 Composition API 并结合 useEditor 方法。

  1. <template>
  2. <editor-content :editor="editor" />
  3. </template>
  4. <script>
  5. import { useEditor, EditorContent } from '@tiptap/vue-3'
  6. import StarterKit from '@tiptap/starter-kit'
  7. export default {
  8. components: {
  9. EditorContent,
  10. },
  11. setup() {
  12. const editor = useEditor({
  13. content: "<p>I'm running Tiptap with Vue.js. 🎉</p>",
  14. extensions: [StarterKit],
  15. })
  16. return { editor }
  17. },
  18. }
  19. </script>

或者,你也可以使用 Vue 3 的 <script setup> 语法

  1. <template>
  2. <editor-content :editor="editor" />
  3. </template>
  4. <script setup>
  5. import { useEditor, EditorContent } from '@tiptap/vue-3'
  6. import StarterKit from '@tiptap/starter-kit'
  7. const editor = useEditor({
  8. content: "<p>I'm running Tiptap with Vue.js. 🎉</p>",
  9. extensions: [StarterKit],
  10. })
  11. </script>

将其添加到你的应用

现在,替换 src/App.vue 文件的内容,并使用 Tiptap 组件:

  1. <template>
  2. <div id="app">
  3. <tiptap />
  4. </div>
  5. </template>
  6. <script>
  7. import Tiptap from './components/Tiptap.vue'
  8. export default {
  9. name: 'App',
  10. components: {
  11. Tiptap,
  12. },
  13. }
  14. </script>

你现在应该可以在浏览器中看到 Tiptap 了。给自己点个赞吧!:)

使用 v-model(可选)

你可能已经习惯在表单中使用 v-model 进行数据绑定,Tiptap 也支持这种方式。以下是如何在 Tiptap 中使用 v-model 的示例:

<p>A Vue.js wrapper component for Tiptap to use <code>v-model</code>.</p>

index.vue

  1. <template>
  2. <div>
  3. <editor v-model="content" />
  4. <div class="output-group">
  5. <label>Content</label>
  6. <code>{{ content }}</code>
  7. </div>
  8. </div>
  9. </template>
  10. <script>
  11. import Editor from './Editor.vue'
  12. export default {
  13. components: {
  14. Editor,
  15. },
  16. data() {
  17. return {
  18. content: '<p>A Vue.js wrapper component for Tiptap to use <code>v-model</code>.</p>',
  19. }
  20. },
  21. }
  22. </script>