vue代码编辑模块

vue代码编辑模块

自定义代码编译器,自定义代码编写。

在这里插入代码片
```<template>
  <div class="monaco-container" ref="codeEditor"></div>
</template>
<script>
import * as monaco from "monaco-editor"
const defaultOptions = {
  selectOnLineNumbers: true,
  roundedSelection: false,
  wordWrap: 'on', // 自动换行 'on','off'
  language: 'java', // 常用的有css/html/java/javascript/json/csharp(.net)'
  theme: 'vs', // vs, hc-black, or vs-dark
  formatOnPaste: true,
  formatOnType: true,
  glyphMargin: true, //字形边缘
  fontSize: 14, // 字体大小
  readOnly: false // 只读
}
export default {
  name: 'MonacoEditor',
  props: {
    options: {
      type: Object,
      default: () => { }
    },
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      content: this.value,
      monacoEditor: null
    }
  },
  watch: {
    value(val) {
      this.content = val
    },
    options: {
      handler(val) {
        this.monacoEditor.updateOptions({ ...val })
      },
      deep: true
    }
  },
  mounted() {
    this.initEditor()
  },
  methods: {
    initEditor() {
      // 初始化编辑器实例
      this.monacoEditor = monaco.editor.create(this.$refs['codeEditor'], {
        value: this.content,
        autoIndex: true,
        ...defaultOptions,
        ...this.options
      })
      // 监听编辑器content变化
      this.monacoEditor.onDidChangeModelContent(() => {
        this.$emit('input', this.monacoEditor.getValue())
      })
    },
    changeEditor(data) {
      this.monacoEditor.setValue(data.value)
      this.monacoEditor.updateOptions({ ...data.options })
    },
    insert(text) {
      text = text || ''
      var position = this.monacoEditor.getPosition();
      this.monacoEditor.executeEdits('', [
        {
          range: {
            startLineNumber: position.lineNumber,
            startColumn: position.column,
            endLineNumber: position.lineNumber,
            endColumn: position.column
          },
          text: text
        }
      ]);
    },
  }
}

</script>
<style lang="scss" scoped>
.monaco-container {
  height: 100%;
  width: 100%;
  overflow: hidden;
}
</style>


第二步 获取到数据页面渲染

在这里插入代码片

              <el-button v-for="item in detailBtnsList" :key="item.code"
                @click="customBtnsHandel(item, scope.row, scope.$index)">{{ item.value }}</el-button>
            </template>
script部分
getScriptFunc(str) {
    let func = null
    try {
      func = eval(str)
      return func
    } catch (error) {
      console.log(error);
      return false
    }
  },
customBtnsHandel(item, row, index) {
      const parameter = {
        data: row,
        index,
        request: this.request,
        toast: this.$message,
        refresh: this.initData
      }
      const func = this.getScriptFunc.call(this, item.func)
      if (!func) return
      func.call(this, parameter) // 解析函数并且传递参数
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值