[英]How to work with Template literals with expres and monaco-editor
我正在嘗試使用monaco編輯器通過Web面板編輯源代碼。
在加載使用模板文字的文件之前,它工作正常。
index.js:
function processFile(inputFile) {
let fs = require('fs'),
path = require('path'),
rl = fs.readFileSync(path.join(__dirname,'../','../','../','apps/',inputFile));
return rl.toString('utf8');
}
router.get('/', function (req, res) {
res.render('index', { title: 'Hey', message: 'Hello there!', test: processFile('libs/ButtonTest.js') })
});
index.pug:
doctype html
html
head
title= title
script(src='monaco/vs/loader.js')
body
h1= message
div(id='container', style='width: 800px;height: 800px;')
script.
require.config({paths: {"vs": "monaco/vs"}});
require(["vs/editor/editor.main"], function() {
var model = monaco.editor.createModel(`!{test}`,'javascript');
var editor = monaco.editor.create(document.getElementById("container"));
editor.setModel(model)
});
我曾考慮過使用regexp代替Template Literal,但是我認為這不是最好的方法。 這是行不通的示例:
var test = [];
test.push("var t = 'test t';");
test.push('var c = "test c";');
test.push('var o = `test o ${c}, ${t}`;');
router.get('/', function (req, res) {
res.render('index', { title: 'Hey', message: 'Hello there!', test: test.join('\r\n') })
});
來自瀏覽器的錯誤: Uncaught SyntaxError: Unexpected identifier
錯誤Uncaught SyntaxError: Unexpected identifier
我發現一種解決方案是JSON.stringify。
.pug文件:
...
var d = !{test};
var model = monaco.editor.createModel(d.join('\r\n'),'javascript');
...
index.js
var test = [];
test.push("var t = 'test t';");
test.push('var c = "test c";');
test.push('var o = `test o ${c}, ${t}`;');
let c = JSON.stringify(test);
router.get('/', function (req, res) {
res.render('index', { title: 'Hey', message: 'Hello there!', test: c })
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.