JS代碼編譯器Monaco使用方法
前言
我的需求是可以語法高亮、函數(shù)提示功能、自動換行、代碼折疊
Monaco
Monaco是微軟家的,支持的語言很多,還有縮略地圖,有時候提示不好用然后包體很大。
The Monaco Editor is the code editor that powers VS Code.

使用方法官網(wǎng)
[官方文檔](https://microsoft.github.io/monaco-editor/index.html)
[在線demo](https://github.com/Microsoft/monaco-editor-samples)
[github](https://github.com/Microsoft/monaco-editor)
安裝
yarn add monaco-editor | npm install monaco-editor
引入
import * as monaco from 'monaco-editor' // 包體很大了 但是demo可以跑起來
//自定義一些提示函數(shù)
const suggestions = [
{
label: 'split_chinese',
insertText: 'split_chinese(inputString,language);', // 不寫的時候不展示。。
detail:
'inputString:need split string\n' +
'language:\nCH_T:traditional Chinese\nCH_S:Chinese Simplified\n HK_T:Hong Kong Traditional\nTW_T:Taiwan Traditional\n'
},
{
label: 'uuid',
insertText: 'var uuid = uuid();',
detail: 'generate uuid'
},
{
label: 'HashMap',
insertText: 'var hashMap = new HashMap();',
detail: 'create hash object'
}
]
初始化
mounted() {
monaco.languages.registerCompletionItemProvider('JavaScript', {
provideCompletionItems() {
return {
suggestions: suggestions
}
},
triggerCharacters: [' ', '.'] // 寫觸發(fā)提示的字符,可以有多個
})
let self = this
setTimeout(function () {
self.init()
}, 50) //因為父組件還未傳參 子組件已經(jīng)渲染
}
//初始化方法
init(script) {
let self = this
if (script) this.code = script
self.$refs.container.innerHTML = ''
var editor = monaco.editor.create(this.$refs.container, {
value: this.code,
language: 'javascript',
minimap: {
enabled: false
},
fontSize: '12px',
fixedOverflowWidgets: true // 超出編輯器大小的使用fixed屬性顯示
})
editor.onDidChangeModelContent(function () {
self.$emit('update:code', editor.getValue()) //用來監(jiān)聽編輯器內(nèi)容變化,將內(nèi)容傳給父組件
})
}
html
<template> <div ref="container" class="monaco"></div> </template>
css
<style scoped>
.monaco {
width: 95%;
height: 400px;
border: 1px solid #dcdfe6;
text-align: left;
margin-right: 20px;
border-radius: 4px;
}
</style>
運行效果

缺點
我的推翻了,不想再跑一下,代碼還在就寫一個demo。運行還是可以的(有客戶使用但也反饋不好用,是我自己的鍋,不配使用Monaco)真的很難用,特別是提示的功能,一般情況下是沒有提示的。然后一個包很大,好像有3.9G(嚴(yán)重)。可能沒有按需引入,但是不引入沒有提示功能,自定義函數(shù)提示。還有webpack配置,來回折騰!
以上就是JS編譯器Monaco使用教程的詳細內(nèi)容,更多關(guān)于JS編譯器Monaco的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
javascript 通用簡單的table選項卡實現(xiàn)
鑒于UI妹妹每次交付過來的選項卡都夾帶了多多少少的js,而且每遇到選項卡就加一點js,造成垃圾低劣代碼逐漸堆積過多了,一直想做一個通用簡潔的選項卡庫。2010-05-05
微信小程序tab切換可滑動切換導(dǎo)航欄跟隨滾動實現(xiàn)代碼
這篇文章主要介紹了微信小程序tab切換可滑動切換導(dǎo)航欄跟隨滾動實現(xiàn)代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09
JavaScript中去掉數(shù)組中的重復(fù)值的實現(xiàn)方法
百度面試時問的一道題目,蠻常規(guī)的,但是當(dāng)時自己的回答挺差勁的?,F(xiàn)在總結(jié)記錄下~2011-08-08
JS大坑之19位數(shù)的Number型精度丟失問題詳解
這篇文章主要介紹了JS大坑之19位數(shù)的Number型精度丟失問題,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
javascript中json對象json數(shù)組json字符串互轉(zhuǎn)及取值方法
這篇文章主要介紹了javascript中json對象json數(shù)組json字符串互轉(zhuǎn)及取值方法,需要的朋友可以參考下2017-04-04

