您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“vue3如何使用vue-codemirror插件”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
npm install vue-codemirror --save // cnpm install vue-codemirror --save
如果運(yùn)行官網(wǎng)例子時(shí), 報(bào)錯(cuò):
@codemirror/lang-javascript
@codemirror/theme-one-dark
可以在終端中安裝對(duì)應(yīng)文件, 解決問(wèn)題
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語(yǔ)言切換
不足, 沒有滿足代碼提示
組件代碼 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() { // 數(shù)據(jù) 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)建({擴(kuò)展}) }); // 方法 // 失去焦點(diǎn)時(shí),使用已編輯的代碼 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插件”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。