react使用ace代码编辑器

文章介绍了如何在Web应用中使用ACE编辑器,提供了两种方法:一是通过CDN引入,二是本地导入。同时,详细展示了如何设置编辑器的语言模式、主题,以及在React项目中使用react-ace组件的步骤,并强调了需要设置编辑器的高度和宽度。
摘要由CSDN通过智能技术生成

参考:ace官网使用指南ace-buildsace-cdnreact-acereact-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%' }} // 必须得设置宽高
/>

效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值