参考:ace官网使用指南,ace-builds,ace-cdn,react-ace,react-ace编辑器 —— 快速使用,最好用的web端代码文本编辑器ACE
方法一
<!-- 第一种:使用cdn上的资源 -->
<script src="https://cdn.bootcdn.net/ajax/libs/ace/1.14.0/ace.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/ace/1.14.0/mode-golang.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/ace/1.14.0/theme-twilight.js"></script>
<!-- 第二种:需要把ace-builds里面的src文件copy到自己本地目录下 -->
<script src="../static/ace/ace.js"></script>
<script src="../static/ace/mode-golang.js"></script>
<script src="../static/ace/theme-twilight.js"></script>
const editor = ace.edit('ace-editor');
ace.config.set('basePath', 'https://unpkg.com/ace-builds@1.4.6/src-noconflict');
// 设置语言和主题
editor.session.setMode('ace/mode/golang');
editor.setTheme('ace/theme/twilight');
// 设置值
editor.getSession().setValue(res?.result.extend_script);
// 读取值
editor.getSession().getValue();
// 必须得设置高度
<div id="ace-editor" style={{ height: 600 }} />
方法二
需要先安装库yarn add react-ace ace-builds
。
import 'ace-builds/src-noconflict/mode-golang';
import 'ace-builds/src-noconflict/theme-twilight';
<AceEditor
name="ace-editor"
mode="golang"
theme="twilight"
value={extendCode}
onChange={(value: string) => setExtendCode(value)}
style={{ height: 600, width: '100%' }} // 必须得设置宽高
/>
效果: