您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇内容介绍了“vue3如何使用vue-codemirror插件”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
npm install vue-codemirror --save // cnpm install vue-codemirror --save
如果运行官网例子时, 报错:
@codemirror/lang-javascript
@codemirror/theme-one-dark
可以在终端中安装对应文件, 解决问题
npm i @codemirror/lang-javascript npm i @codemirror/theme-one-dark
<template> <codemirror v-model="code" placeholder="Code gose here..." : :autofocus="true" :indent-with-tab="true" :tabSize="2" :extensions="extensions" @ready="log('ready', $event)" @change="log('change', $event)" @focus="log('focus', $event)" @blur="log('blur', $event)" /> </template> <script> import { Codemirror } from "vue-codemirror"; import { javascript } from "@codemirror/lang-javascript"; import { oneDark } from "@codemirror/theme-one-dark"; import { ref } from "vue"; export default { components: { Codemirror, }, setup() { const code = ref(`console.log('Hello, world!')`); const extensions = [javascript(), oneDark]; return { code, extensions, log: console.log, }; }, }; </script>
代码编辑区
支持代码编辑区, 满足白天/黑夜主题切换, 满足c++/python语言切换
不足, 没有满足代码提示
组件代码 vue3
<template> <button @click="changeTheme($event)">黑夜</button> <button @click="changeMode($event)">C++</button> <codemirror v-model="code" placeholder="Code gose here..." : :mode="mode" :spellcheck="spellcheck" :autofocus="autofocus" :indent-with-tab="indentWithTab" :tabSize="tabSize" :extensions="extensions" @ready="log('ready', $event)" @change="log('change', $event)" @focus="log('focus', $event)" @blur="useEditedCode" /> </template> <script> import { Codemirror } from "vue-codemirror"; import { python } from "@codemirror/lang-python"; import { cpp } from "@codemirror/lang-cpp"; import { oneDark } from "@codemirror/theme-one-dark"; import "codemirror/addon/hint/show-hint.css"; import { reactive, ref, toRefs } from "vue"; export default { components: { Codemirror, }, setup() { // 数据 const code = ref(``); let selectValue = "cpp"; let dateTime = "黑夜"; const options = reactive({ style: { height: "400px" }, mode: "text/x-c++src", spellcheck: true, autofocus: true, indentWithTab: true, tabSize: 2, extensions: [cpp(), oneDark], //传递给CodeMirror EditorState。创建({扩展}) }); // 方法 // 失去焦点时,使用已编辑的代码 function useEditedCode() { console.log("@@@blur@@@code:", code.value); console.log("@@@blur@@@cpp:", cpp); } // 改变主题 function changeTheme(e) { console.log("options.extensions:", options.extensions); if (e.target.innerHTML === "黑夜") { options.extensions = []; dateTime = e.target.innerHTML = "白天"; } else { options.extensions = [oneDark]; dateTime = e.target.innerHTML = "黑夜"; } } // 改变模式 function changeMode(e) { console.log("selectValue:", selectValue); if (selectValue === "cpp") { if (dateTime === "黑夜") options.extensions = [python(), oneDark]; else options.extensions = [python()]; selectValue = "python"; e.target.innerHTML = "python"; options.mode = "text/x-python"; } else { if (dateTime === "黑夜") options.extensions = [cpp(), oneDark]; else options.extensions = [cpp()]; selectValue = "cpp"; e.target.innerHTML = "C++"; options.mode = "text/x-c++src"; } } // 返回 return { code, selectValue, dateTime, ...toRefs(options), log: console.log, useEditedCode, changeTheme, changeMode, }; }, }; </script>
“vue3如何使用vue-codemirror插件”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。