赞
踩
npm install ace-builds --save-dev
<div class="ace-container"> <div class="event-panel"> <div> <div class="_li"> <label class="title">语言</label> <el-select class="_li_val" v-model="modePath" @change="handleModelPathChange" size="mini" value-key="name"> <el-option v-for="mode in modeArray" :key="mode.name" :label="mode.name" :value="mode.path"> </el-option> </el-select> </div> <div class="_li"> <label class="title">参数</label> <el-input class="_li_val" placeholder="请输入内容" size="mini" v-model="inputDa" clearable> </el-input> </div> </div> </div> <div class="ace-editor" ref="ace"></div> <div class="_li result ma-t-20"> <el-button class="ma-r-20" type="primary" size="mini" plain @click="debEvel">调试</el-button> <label class="title">结果:</label> <span class="item-li" :class="isValidate ? 'item-c' : 'item-warning'">{{ resultDa }}</span> </div> </div> </template> <script> import ace from "ace-builds"; import "ace-builds/src-noconflict/snippets/javascript"; import "ace-builds/src-noconflict/snippets/html"; import "ace-builds/src-noconflict/snippets/css"; import "ace-builds/src-noconflict/snippets/scss"; import "ace-builds/src-noconflict/snippets/json"; import "ace-builds/src-noconflict/snippets/java"; import "ace-builds/src-noconflict/snippets/text"; import "ace-builds/src-noconflict/theme-xcode"; import "ace-builds/src-noconflict/mode-javascript"; import "ace-builds/src-noconflict/mode-json" import "ace-builds/src-noconflict/theme-monokai"; import "ace-builds/src-noconflict/ext-language_tools"; // 解决build打包时包多大的问题(webpack-resolver原始包里引入所以的js文件,这里只引入了我们需要的js) ace.config.setModuleUrl('ace/mode/javascript_worker', require('file-loader?esModule=false!ace-builds/src-noconflict/worker-javascript.js')) ace.config.setModuleUrl('ace/theme/xcode', require('file-loader?esModule=false!ace-builds/src-noconflict/theme-xcode.js')) ace.config.setModuleUrl('ace/ext/language_tools', require('file-loader?esModule=false!ace-builds/src-noconflict/ext-language_tools.js')) ace.config.setModuleUrl('ace/snippets/javascript', require('file-loader?esModule=false!ace-builds/src-noconflict/snippets/javascript.js')) ace.config.setModuleUrl('ace/mode/json_worker', require('file-loader?esModule=false!ace-builds/src-noconflict/worker-json.js')) const modeArray = [ { name: "JavaScript", path: "ace/mode/javascript", }, { name: "Json", path: "ace/mode/json", }, { name: "Text", path: "ace/mode/text", }, ]; export default { props: { value: String, }, mounted() { this.aceEditor = ace.edit(this.$refs.ace, { maxLines: 20, minLines: 10, fontSize: 14, value: this.value ? this.value : "", theme: this.themePath, mode: this.modePath, // wrap: this.wrap, tabSize: 4, }); // 激活自动提示 this.aceEditor.setOptions({ enableSnippets: true, enableLiveAutocompletion: true, enableBasicAutocompletion: true, }); // // 事件监听 this.aceEditor.getSession().on("change", this.change); // 默认换行 this.aceEditor.getSession().setUseWrapMode(true); // 默认代码 this.aceEditor.getSession().setValue(this.modelAce); }, data() { return { aceEditor: null, toggle: true, wrap: true, themePath: "ace/theme/monokai", modePath: "ace/mode/javascript", modeArray: modeArray, resultDa: "", inputDa: '', modelAce: '\nfunction hanlde(data){\n\t//请输入处理脚本\n\t\n\treturn data;\n}', isValidate: true, }; }, methods: { toggleConfigPanel() { this.toggle = !this.toggle; }, change() { let das = this.aceEditor.getSession().getValue() console.log(das, '监听输入') }, // 语言 handleModelPathChange(modelPath) { this.aceEditor.getSession().setMode(modelPath); }, // 调试 debugger debEvel() { try { this.isValidate = true; const func = this.aceEditor.getSession().getValue(); const funs = '(' + func + ')(' + JSON.stringify(this.inputDa) + ')'; this.resultDa = eval(funs); } catch { this.isValidate = false; this.resultDa = "执行异常!请检查执行代码块!"; } }, }, }; </script> <style lang='scss' scoped> .ma-t-20 { margin-top: 20px; } .ma-l-20 { margin-left: 20px; } .ma-r-20 { margin-right: 20px; } .ace-editor { // height: 300px !important; width: 600px; } .item-warning { color: red; } .item-c { color: #333; } .ace-container { .event-panel { ._li { display: flex; margin: 20px 10px; ._li_val { width: 200px; } .title { margin: 0 10px; font-size: 14px; } } } } </style>
在线效果:https://www.w3cschool.cn/tryrun/runcode?lang=javascript
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。