当前位置:   article > 正文

【codemirror】Json编辑器使用总结_codemirror json

codemirror json

目录

1.前言:

2.使用步骤:

        拷贝admin项目中关于这个的,vue文件

        npm下载对应的包

        接下来可以注册为全局组件

        暴露接口,通过:value传输数据给组件,就可以简单使用。

3.注意事项 

4.封装成公共组件,可直接使用,基于vue2,运行后按照提示npm下载包


1.前言:

使用的是vue-template-admin中的Json编辑器。目的是展示后端传过来的Json字符串数据。


2.使用步骤:

拷贝admin项目中关于这个的.vue文件

npm下载对应的包

接下来可以注册为全局组件

暴露接口,通过:value传输数据给组件,就可以简单使用。


3.注意事项 

1.设置编辑器的样式之类的,需要再引用文件,在node_moudules里面找codemirror文件夹。

2.可以设置的地方很多,可以参考官网,列举一些常用属性:

mode: "text/groovy",    //实现groovy代码高亮
mode: "text/x-java", //实现Java代码高亮
lineNumbers: true,	//显示行号
theme: "dracula",	//设置主题
lineWrapping: true,	//代码折叠
foldGutter: true,
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
matchBrackets: true,	//括号匹配
//readOnly: true,        //只读

3.除了使用对外暴露的接口外,还可以使用setvalue()方法去赋值


 4.封装成公共组件,可直接使用,基于vue2,运行后按照提示npm下载包

新建JsonEditor.vue文件,然后注册为全局组件:

  1. <template>
  2. <!-- 只需要传入数据即可使用:value -->
  3. <div class="json-editor">
  4. <textarea ref="textarea" />
  5. </div>
  6. </template>
  7. <style scoped>
  8. @import '../../../node_modules/codemirror/lib/codemirror.css';
  9. @import '../../../node_modules/codemirror/theme/dracula.css';
  10. @import '../../../node_modules/codemirror/addon/lint/lint.css';
  11. </style>
  12. <script>
  13. import CodeMirror from 'codemirror'
  14. import 'codemirror/mode/javascript/javascript'
  15. import 'codemirror/addon/lint/lint'
  16. import 'codemirror/addon/lint/json-lint'
  17. // eslint-disable-next-line import/no-webpack-loader-syntax
  18. require('script-loader!jsonlint')
  19. export default {
  20. name: 'JsonEditor',
  21. /* eslint-disable vue/require-prop-types */
  22. props: ['value'],
  23. data () {
  24. return {
  25. jsonEditor: false
  26. }
  27. },
  28. watch: {
  29. value (value) {
  30. const editorValue = this.jsonEditor.getValue()
  31. if (value !== editorValue) {
  32. this.jsonEditor.setValue(JSON.stringify(this.value, null, 2))
  33. }
  34. }
  35. },
  36. mounted () {
  37. this.jsonEditor = CodeMirror.fromTextArea(this.$refs.textarea, {
  38. lineNumbers: true,
  39. mode: 'application/json',
  40. gutters: ['CodeMirror-lint-markers'],
  41. theme: 'rubyblue',
  42. lint: true,
  43. readOnly: true // 只读
  44. })
  45. this.jsonEditor.setValue(JSON.stringify(this.value, null, 2))
  46. this.jsonEditor.on('change', cm => {
  47. this.$emit('changed', cm.getValue())
  48. this.$emit('input', cm.getValue())
  49. })
  50. },
  51. methods: {
  52. getValue () {
  53. return this.jsonEditor.getValue()
  54. }
  55. }
  56. }
  57. </script>
  58. <style lang="scss" scoped>
  59. .json-editor {
  60. text-align-last: left;
  61. // width: 100%;
  62. // height: 100%;
  63. position: relative;
  64. ::v-deep {
  65. .CodeMirror {
  66. height: auto;
  67. min-height: 300px;
  68. }
  69. .CodeMirror-scroll {
  70. min-height: 300px;
  71. }
  72. .cm-s-rubyblue span.cm-string {
  73. color: #F08047;
  74. }
  75. }
  76. }
  77. </style>


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/739718
推荐阅读
相关标签
  

闽ICP备14008679号