首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Vue 3中使用V-模型和<script setup>的提示

在Vue 3中使用V-模型和<script setup>的提示
EN

Stack Overflow用户
提问于 2022-06-08 16:37:47
回答 1查看 747关注 0票数 1

我试图在Vue.js中使用tiptap和创建单个文件组件的<script setup>方法。

TextEditor.vue

代码语言:javascript
复制
<template>
  <editor-content :editor="editor" class="editor" />
</template>

<script lang="ts" setup>
import { useEditor, EditorContent } from '@tiptap/vue-3'
import StarterKit from '@tiptap/starter-kit'

const props = defineProps({
  modelValue: {
    type: String,
    default: "",
  }
})

const emit = defineEmits(['update:modelValue'])

const editor = useEditor({
  content: props.modelValue,
  extensions: [StarterKit],
  onUpdate: ({editor}) => {
    let content = editor.getHTML()
    emit('update:modelValue', content)
  }
})
</script>

然后,我像这样使用这个组件:

代码语言:javascript
复制
<template>
  <text-editor v-model="myModel.content" />
</template>

这在定义<text-editor>之后加载myModel.content时起作用。

但是,如果在从我的数据库API设置<text-editor>之前加载myModel.content,那么文本内容仍然是空的。通过查看tiptap文档中的示例,我了解到,当使用以下内容更改watch时,需要使用editor来更新props.modelValue

代码语言:javascript
复制
watch(() => props.modelValue, (newValue, oldValue) => {
  const isSame = newValue === oldValue
  console.log(`Same: ${isSame}`)
  if (isSame) {
    return
  }
  editor.commands.setContent(newValue, false)
})

但是,在上面的片段中,editor是一个ShallowRef类型,没有引用commands来调用setContent

当使用<script setup>方法加载提示时,获得上述示例的最佳方法是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-08 17:33:54

您需要使用.value访问ref实际值。

代码语言:javascript
复制
editor.value?.commands.setContent('<p>test</p>', false)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72549319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档