vue.js

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript类库 > vue.js > vue.js codeMirror代码编辑器

vue.js集成codeMirror代码编辑器的详细代码

作者:renpingsheng788

这篇文章主要介绍了vue.js集成codeMirror代码编辑器的相关资料,代码分为前端代码和后端代码,感兴趣的朋友一起看看吧

1.前端代码

<link href="https://cdn.bootcss.com/codemirror/5.48.4/codemirror.css" rel="external nofollow"  rel="stylesheet">
<script src="https://cdn.bootcss.com/codemirror/5.48.4/codemirror.js"></script>
<script src="https://cdn.bootcss.com/codemirror/5.48.4/mode/python/python.js"></script>
<div id="app" style="margin-top: 60px;">
    <el-row :gutter="40">
        <el-col :span="20" :offset="2">
            <div style="border: 1px solid #ddd;" id="div1">
                <textarea id="editor_demo"></textarea>
            </div>
        </el-col>
        <el-col :span="2" :offset="20" style="margin-top: 20px;">
            <el-button type="primary" @click="handleAdd">添加</el-button>
        </el-col>
    </el-row>
</div>
<script type="text/javascript">
    new Vue({
        el: '#app',
        data: {
            editor: null
        },
        mounted() {
            this.init()
        },
        methods: {
            init() {
                this.editor = CodeMirror.fromTextArea(document.getElementById("editor_demo"), {
                  lineNumbers: true,
                  indentWithTabs: true,
                  mode: "python",
                  matchBrackets: true
                });
                this.editor.setSize('auto','600px');
            },
            handleAdd() {
                axios.post(site_url + "create_blog/", {"content": this.editor.getValue()}).then(res => {
                    if (res.data.result) {
                        this.$message.success('添加内容成功');
                    } else {
                        this.$message.error('添加内容失败');
                    }
                }, 'json');
            }
        }
    })
</script>

2.后端代码

def create_blog(request):
    data = json.loads(request.body)
    content = data.get("content")
    print(content)
    ...
    return JsonResponse({"result": True})

显示效果

1133627-20191101145206217-1482956911.png

到此这篇关于vue.js集成codeMirror代码编辑器的文章就介绍到这了,更多相关vue.js codeMirror代码编辑器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文