您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“vue3中怎么使用vue-codemirror插件”,內容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“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>
代碼編輯區(qū)
支持代碼編輯區(qū), 滿足白天/黑夜主題切換, 滿足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。創(chuàng)建({擴展}) }); // 方法 // 失去焦點時,使用已編輯的代碼 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插件”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。