首页 > 解决方案 > 使用 CodeMirror。“无法设置未定义的属性‘modeOption’”

问题描述

目前遇到了这个问题。

const configDataNode = document.getElementById('config_data');
    const editor = CodeMirror.fromTextArea(
      document.getElementById('config_data_editor'),
      {
        // lineNumbers: true,
        mode: 'javascript',
        // tabSize: 2,
        // indentWithTabs: true,
        // value: JSON.stringify(gon.config.initialData, 2, 2),
      },
    );
    editor.on('change', changeObject => {
      const {text} = changeObject;
      configDataNode.value = text;
    });

这是我的代码。

标签: javascript

解决方案


这是由于TextArea对象的值字段undefined而发生的,因此CodeMirror codemirror.js库内部options.value正在初始化doc变量也未定义,因此稍后在源modeOption中与文档子字段匹配(如果文档未定义,对象键可能如何存在)。

var textarea_editor = document.getElementById("RichTextArea");

// so explicitly assign value to textarea object.
textarea_editor.value = "";


this.editor = CodeMirror.fromTextArea(textarea_editor, {
     tabSize: 4,
     mode: 'text/plain',
     theme: 'default',
     lineNumbers: true,
     styleActiveSelected: true,
     styleActiveLine: true,
     indentWithTabs: true,
     matchBrackets: true,
     highlightMatches: true,
});

希望这可以帮助某人。


推荐阅读