【发布时间】:2018-05-16 20:30:01
【问题描述】:
新的 javascript 模板语法很棒。超级可读且功能强大。我想开始使用它。
我试过这个模板:
function addGalleryItem(imageData, file) {
try {
var template = `
<section class="imageGalleryItem">
<img src="${imageData}"/>
<div class="itemTools" id="${file.name}">
<input type="text" class="description" name="description" placeholder="Description"/> <br />
<input type="button" name="mainImage" value="Main Image" onclick="makeMain(this)"/>
<input type="button" name="remove" value="Remove" onclick="removeImage(this)"/>
</div>
</section>
`;
} catch {
var template = '<section class="imageGalleryItem">' +
' <img src="' + imageData + '" />' +
' <div class="itemTools" id="' + file.name + '">' +
' <input type="text" class="description" name="description" placeholder="Description"/>'+
' <br />' +
' <input type="button" name="mainImage" value="Main Image" onclick="makeMain(this)"/>' +
' <input type="button" name="remove" value="Remove" onclick="removeImage(this)"/>' +
' </div>' +
'</section> ';
}
$('#imageGallery').append(template);
}
但亲爱的 IE 会因为反引号 (`) 而抱怨语法错误。 MSDN's article on the subject 推销 Edge 的强大功能,并没有提到要为 IE 做什么。
现在有没有办法直接将新模板语法用于生产用途?还是我们被困在转译中?
【问题讨论】:
-
使用 try...catch -.- 无法捕获语法错误
-
@Gothdo 是的,我知道。虽然习惯上会展示我们尝试过的东西,但我想不出还有什么可以尝试的。
-
如果没有中间步骤,您如何使用浏览器不支持的功能?
-
像这样的东西正是 Babel 解决的问题。