请问在哪里设置编辑器初始值(setValue),after 也不成功?

本贴最后更新于 1133 天前,其中的信息可能已经斗转星移

版本号:

"vditor": "^3.8.3",

目前已尝试如下方法:

new 对象之后初始化

this.vditor = new Vditor('vditor', options);
      this.vditor.focus();

设置内容初始化:

this.vditor.setValue('# 这是初始内容', false);

不成功,报如下错误:

Cannot read property 'SpinVditorSVDOM' of undefined

参考了如下帖子:
https://github.com/Vanessa219/vditor/issues/273#issuecomment-609386420
Issue #523 · Vanessa219/vditor
说是在 after()里设置,但是 after 里面,也设置不成功,vditor 对象还是 undefine 的状态。
代码:

request.onload = that.onloadCallback;
            request.send(formData);
          }
        },
        after() {
          console.log('--- init content ----');
          // vditor.setValue('# 哈哈', true);
          if (this.vditor) {
            console.log('---- ok vditor --');
          } else {
            console.log('--- empty vditor ---');
          }
        }

控制台输出 empty vditor。
image.png

  • Vditor

    Vditor 是一款浏览器端的 Markdown 编辑器,支持所见即所得、即时渲染(类似 Typora)和分屏预览模式。它使用 TypeScript 实现,支持原生 JavaScript、Vue、React 和 Angular。

    313 引用 • 1667 回帖 • 1 关注

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...
  • 需要方在 new Vditorafter 参数中

    1 回复
  • rayjohnttttt 1 评论
    作者

    已经是 new 里的方法,以下是完整代码:

      mounted() {
        this.initVditor()
        this.$nextTick(() => {
          this.isLoading = false
        })
      },
    
    initVditor() {
          const that = this;
          const options = {
            width: '100%',
            height: '0',
            tab: '\t',
            counter: '999999',
            typewriterMode: true,
            mode: 'sv',
            preview: {
              delay: 100,
              show: !this.isMobile
            },
            outline: true,
            upload: {
              max: 5 * 1024 * 1024,
              // linkToImgUrl: 'https://sm.ms/api/upload',
              handler(file) {
                let formData = new FormData();
                for (let i in file) {
                  formData.append('smfile', file[i]);
                }
                let request = new XMLHttpRequest()
                request.open('POST', 'https://sm.ms/api/upload')
                request.onload = that.onloadCallback;
                request.send(formData);
              }
            },
            after() {
              console.log('--- init content ----');
              // vditor.setValue('# 哈哈', true);
              if (this.vditor) {
                console.log('---- ok vditor --');
              } else {
                console.log('--- empty vditor ---');
              }
            }
          };
          this.vditor = new Vditor('vditor', options);
          this.vditor.focus();
    
    1 操作
    rayjohnttttt 在 2021-03-19 17:42:26 更新了该回帖
    应该是在 focus 这里报错的
    Vanessa
  • rayjohnttttt
    作者

    你好。我贴上代码了 能帮忙看看是为什么吗

  • rayjohnttttt
    作者

    focus 注释掉了 还是不成功。

  • rayjohnttttt
    作者

    观察到一个跟版本有关的奇怪现象:

    版本 3.2.12,在初始化之后,是可以 setValue 的,但是会报 render undefined 的错误。

    但这个版本的 after,setValue 的时候,vditor 还是空的。

    3.2.12 的代码:

      mounted() {
        // this.initVditor()
        this.initVditor();
        this.$nextTick(() => {
          this.isLoading = false
        })
    
        console.log('---- in mounted ---');
        if(this.contentEditor) {
          console.log('----- not empty -----');
          this.contentEditor.setValue('# 在mounted中, 哈哈', true);
        } else {
          console.log('----- empty -----');
        }
      },
    

    有可能是回归 bug 吗?

  • rayjohnttttt 1 评论
    作者

    已解决。经过反复尝试,还是得用本地变量,就可以解决这个问题。我是在 vue 中使用的。不能直接 this.vditor.setValue.

          const vditor = new Vditor('vditor', {
            width: this.isMobile ? '100%' : '80%',
            height: '0',
            tab: '\t',
            counter: '999999',
            typewriterMode: true,
            mode: 'sv',
            preview: {
              delay: 100,
              show: !this.isMobile
            },
            after() {
              console.log('--- init content ---');
              if(vditor) {
                console.log('--- not empty ---');
                vditor.setValue('# 哈哈', false);
              } else {
                console.log('--- empty --');
              }
            }
          });
          this.contentEditor = vditor;
    

    成功 setValue。

    我猜测应该是跟 this 的生命周期有关系。

    ReactJs 可能也会遇到这个问题吧 😄 。

    但是我看另一个项目(使用 vditor 3.2.12 版本)使用了 vditor,他的代码如下,可以 setValue:

    https://github.com/lhlyu/petard (演示地址:https://lhlyu.github.io/petard/#/)

    export default {
      name: 'index',
      components: {
        Upload
      },
      watch: {
        $route (val, oldVal) {
          if (val.name !== 'article-edit') {
            return
          }
          this.loadArticle(val.query.id)
        }
      },
      data () {
        return {
          loading: false,
          dialogCoverVisible: false,
          contentEditor: null,
          categorys: null,
          req: {
            id: 0,
            title: '',
            summary: '',
            cover: '',
            top: 0,
            categoryId: null,
            tags: [],
            content: '',
            kind: 1,
            storeMode: 2,
            state: 1,
            commentState: 1
          }
        }
      },
      methods: {
        init () {
          this.initEdtor()
          this.loadCategorys()
          this.loadArticle(this.$route.query.id)
        },
        initEdtor (value) {
          this.contentEditor = new Vditor('vditor', {
            mode: 'sv',
            minHeight: 550,
            cache: {
              enable: true,
              id: 'petardEditor'
            }
          })
        },
        add () {
          this.req = {
            id: 0,
            title: '',
            summary: '',
            cover: '',
            top: 0,
            categoryId: null,
            tags: [],
            content: '',
            kind: 1,
            storeMode: 2,
            state: 1,
            commentState: 1
          }
          this.contentEditor.setValue('')
        },
        reset () {
          if (this.req.id === 0) {
            this.add()
            return
          }
          this.loadArticle(this.req.id)
        },
        async loadArticle (id) {
          if (!id) {
            return
          }
          this.loading = true
          const result = await this.$request.fetchArticle({
            id: id
          })
          if (result.code) {
            this.$message.warning(result.message)
            this.loading = false
            return
          }
          this.req = result.data
          this.contentEditor.setValue(this.req.content)
          this.loading = false
        },
        async loadCategorys () {
          this.loading = true
          const result = await this.$request.fetchCategorys({
            pageNum: 1,
            pageSize: 20
          })
          if (result.code) {
            this.categorys = []
            this.$message.warning(result.message)
            this.loading = false
            return
          }
          this.categorys = result.data.list
          this.loading = false
        },
        async save () {
          const length = this.contentEditor.getHTML().trim().length
          if (length === 0) {
            this.$message.warning('内容不能为空!')
          }
          if (this.isEmpty(this.req.title, '标题不能为空!')) {
            return
          }
          this.req.content = this.contentEditor.getValue().trim()
          this.loading = true
          const result = await this.$request.fetchEditArticle(this.req)
          if (result.code) {
            this.$message.warning(result.message)
            this.loading = false
            return
          }
          this.$message.success(result.message)
          this.loading = false
        },
        // ------------ handler -----------------
        handlerUpload (data) {
          if (!data) {
            return
          }
          this.req.cover = data.url
        }
      },
      computed: {
        ...mapState(['dict'])
      }
    }
    
    是不是 this 不对?
    Vanessa
请输入回帖内容 ...

推荐标签 标签

  • Laravel

    Laravel 是一套简洁、优雅的 PHP Web 开发框架。它采用 MVC 设计,是一款崇尚开发效率的全栈框架。

    19 引用 • 23 回帖 • 685 关注
  • 外包

    有空闲时间是接外包好呢还是学习好呢?

    26 引用 • 232 回帖 • 6 关注
  • Kafka

    Kafka 是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据。 这种动作(网页浏览,搜索和其他用户的行动)是现代系统中许多功能的基础。 这些数据通常是由于吞吐量的要求而通过处理日志和日志聚合来解决。

    35 引用 • 35 回帖
  • 爬虫

    网络爬虫(Spider、Crawler),是一种按照一定的规则,自动地抓取万维网信息的程序。

    106 引用 • 275 回帖 • 1 关注
  • Q&A

    提问之前请先看《提问的智慧》,好的问题比好的答案更有价值。

    6551 引用 • 29424 回帖 • 246 关注
  • 域名

    域名(Domain Name),简称域名、网域,是由一串用点分隔的名字组成的 Internet 上某一台计算机或计算机组的名称,用于在数据传输时标识计算机的电子方位(有时也指地理位置)。

    43 引用 • 208 回帖 • 2 关注
  • React

    React 是 Facebook 开源的一个用于构建 UI 的 JavaScript 库。

    192 引用 • 291 回帖 • 443 关注
  • HTML

    HTML5 是 HTML 下一个的主要修订版本,现在仍处于发展阶段。广义论及 HTML5 时,实际指的是包括 HTML、CSS 和 JavaScript 在内的一套技术组合。

    103 引用 • 294 回帖
  • 深度学习

    深度学习(Deep Learning)是机器学习的分支,是一种试图使用包含复杂结构或由多重非线性变换构成的多个处理层对数据进行高层抽象的算法。

    40 引用 • 40 回帖
  • Git

    Git 是 Linux Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。

    205 引用 • 357 回帖 • 1 关注
  • Logseq

    Logseq 是一个隐私优先、开源的知识库工具。

    Logseq is a joyful, open-source outliner that works on top of local plain-text Markdown and Org-mode files. Use it to write, organize and share your thoughts, keep your to-do list, and build your own digital garden.

    4 引用 • 55 回帖 • 8 关注
  • Hadoop

    Hadoop 是由 Apache 基金会所开发的一个分布式系统基础架构。用户可以在不了解分布式底层细节的情况下,开发分布式程序。充分利用集群的威力进行高速运算和存储。

    82 引用 • 122 回帖 • 621 关注
  • 友情链接

    确认过眼神后的灵魂连接,站在链在!

    24 引用 • 373 回帖
  • Log4j

    Log4j 是 Apache 开源的一款使用广泛的 Java 日志组件。

    20 引用 • 18 回帖 • 42 关注
  • SpaceVim

    SpaceVim 是一个社区驱动的模块化 vim/neovim 配置集合,以模块的方式组织管理插件以
    及相关配置,为不同的语言开发量身定制了相关的开发模块,该模块提供代码自动补全,
    语法检查、格式化、调试、REPL 等特性。用户仅需载入相关语言的模块即可得到一个开箱
    即用的 Vim-IDE。

    3 引用 • 31 回帖 • 71 关注
  • DevOps

    DevOps(Development 和 Operations 的组合词)是一组过程、方法与系统的统称,用于促进开发(应用程序/软件工程)、技术运营和质量保障(QA)部门之间的沟通、协作与整合。

    40 引用 • 24 回帖
  • iOS

    iOS 是由苹果公司开发的移动操作系统,最早于 2007 年 1 月 9 日的 Macworld 大会上公布这个系统,最初是设计给 iPhone 使用的,后来陆续套用到 iPod touch、iPad 以及 Apple TV 等产品上。iOS 与苹果的 Mac OS X 操作系统一样,属于类 Unix 的商业操作系统。

    84 引用 • 139 回帖
  • RabbitMQ

    RabbitMQ 是一个开源的 AMQP 实现,服务器端用 Erlang 语言编写,支持多种语言客户端,如:Python、Ruby、.NET、Java、C、PHP、ActionScript 等。用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面表现不俗。

    49 引用 • 60 回帖 • 399 关注
  • Webswing

    Webswing 是一个能将任何 Swing 应用通过纯 HTML5 运行在浏览器中的 Web 服务器,详细介绍请看 将 Java Swing 应用变成 Web 应用

    1 引用 • 15 回帖 • 636 关注
  • 链书

    链书(Chainbook)是 B3log 开源社区提供的区块链纸质书交易平台,通过 B3T 实现共享激励与价值链。可将你的闲置书籍上架到链书,我们共同构建这个全新的交易平台,让闲置书籍继续发挥它的价值。

    链书社

    链书目前已经下线,也许以后还有计划重制上线。

    14 引用 • 257 回帖 • 1 关注
  • Java

    Java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的。Java 技术具有卓越的通用性、高效性、平台移植性和安全性。

    3168 引用 • 8207 回帖
  • Redis

    Redis 是一个开源的使用 ANSI C 语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value 数据库,并提供多种语言的 API。从 2010 年 3 月 15 日起,Redis 的开发工作由 VMware 主持。从 2013 年 5 月开始,Redis 的开发由 Pivotal 赞助。

    284 引用 • 247 回帖 • 174 关注
  • CSS

    CSS(Cascading Style Sheet)“层叠样式表”是用于控制网页样式并允许将样式信息与网页内容分离的一种标记性语言。

    180 引用 • 447 回帖 • 1 关注
  • Unity

    Unity 是由 Unity Technologies 开发的一个让开发者可以轻松创建诸如 2D、3D 多平台的综合型游戏开发工具,是一个全面整合的专业游戏引擎。

    25 引用 • 7 回帖 • 247 关注
  • 一些有用的避坑指南。

    69 引用 • 93 回帖 • 1 关注
  • LeetCode

    LeetCode(力扣)是一个全球极客挚爱的高质量技术成长平台,想要学习和提升专业能力从这里开始,充足技术干货等你来啃,轻松拿下 Dream Offer!

    209 引用 • 72 回帖 • 2 关注
  • 又拍云

    又拍云是国内领先的 CDN 服务提供商,国家工信部认证通过的“可信云”,乌云众测平台认证的“安全云”,为移动时代的创业者提供新一代的 CDN 加速服务。

    21 引用 • 37 回帖 • 513 关注