首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >React摩纳哥-编辑打字稿整合

React摩纳哥-编辑打字稿整合
EN

Stack Overflow用户
提问于 2021-12-07 13:49:57
回答 1查看 1.6K关注 0票数 0

根据@摩纳哥编辑器/react的文档,我们需要原始摩纳哥编辑器作为对等依赖。我将其安装为一个dev依赖项,并试图将node_modules/monaco-editor/monaco.d.ts添加到include块中的tsconfig.json中。但在此之后,我仍然无法访问类型声明。我唯一能让它工作的方法就是像这样导入它。

import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';

但是现在,当我试图为editormonaco变量定义它们时,我的类型声明会抛出错误。有人知道热身能让它们正常工作吗?

下面是我在定义类型时的代码。

代码语言:javascript
运行
复制
import { useState, useRef } from 'react';
import Editor from '@monaco-editor/react';
import styles from '../Code.module.css';
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
type Language = {
  languageName: string;
  languageID: number;
};

type EditorProps = {
  language: Language;
  onCodeChange: Function;
};
type Monaco = typeof monaco // to use it as a type for the monaco variable in handleEditorDidMount function

const CodeEditor = ({ language, onCodeChange }: EditorProps) => {
  console.log('language is ', language);

  const [editorValue, setEditorValue] = useState('//some comment');

  const editorRef = useRef(null);

  const monacoRef = useRef(null);

  const handleEditorDidMount = (editor: monaco.editor.IStandaloneCodeEditor, monaco: Monaco) => {
    editorRef.current = editor;
    monacoRef.current = monaco;
    
    console.log(monaco.languages.getLanguages());
    console.log('editor', editor.getModel().getLanguageIdentifier().language);
  };

  const handleEditorValueChange = (value: string|undefined) => {
    //console.log('event is ', event);
    setEditorValue(value);
  };
  const editorOptions: monaco.editor.IStandaloneEditorConstructionOptions = {
    fontSize: 15,
    minimap: {
      enabled: true,
    },
  };
  return (
    <div className={styles.editorBlock}>
      <div className={styles.editorNav}>
        <button>Submit</button>
        <button>Change Language</button>
      </div>
      <Editor
        height="90vh"
        language={language.languageName}
        defaultValue={editorValue}
        onMount={handleEditorDidMount}
        onChange={handleEditorValueChange}
        options={editorOptions}
      />
    </div>
  );
};

在我的handleEditorDidMount函数中,参数摩纳哥抛出错误,不能使用导入作为类型

EN

回答 1

Stack Overflow用户

发布于 2021-12-08 08:17:28

您在代码中做了许多错误的假设。

导入类似普通模块的输入文件的

  1. 不会使输入在您的IDE中可用。
  2. 摩纳哥已经命名了导出,所以使用类似于:import { languages, editor } from "monaco-editor/esm/vs/editor/editor.api";之类的东西。这将导入两个名称空间Editor.
  3. monacolanguages模块react-摩纳哥导出MonacoEditor,而不是Editor.
  4. monaco(正如您导入的那样)只是来自摩纳哥模块的导出对象。你对“摩纳哥类型”的期待是什么?
  5. monaco的引用应该包含什么?React中的引用持有指向对象的指针,这些对象要么是在DOM中实例化的,要么是React组件的实例。让这个引用用于摩纳哥导出毫无意义。
  6. 通常在呈现调用中自动初始化,如:

代码语言:javascript
运行
复制
                <MonacoEditor
                    ref={this.editorRef}
                    language={language}
                    options={opts}
                />

因此,您不需要挂载函数来设置它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70261303

复制
相关文章

相似问题

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