在除IE之外的其他浏览器上工作的模板文字 [英] Template literals working on other browser except IE

查看:81
本文介绍了在除IE之外的其他浏览器上工作的模板文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用javascript并使用模板文字,它正在使用Chrome和Firefox,但它无法在Internet Explorer(IE)上运行。

I am working on javascript and using "Template literals", which is working on the Chrome and Firefox, but it is not working on the Internet Explorer(IE).

var a = 10;
console.log(`${a}`)

推荐答案

模板文字是ES6,IE支持很少 ES6功能。它不支持模板文字。

Template literals are ES6, and IE supports very few ES6 features. It doesn't support template literals.

对于你正在做的事情,只需执行 console.log(a)代替:

For what you're doing, simply do console.log(a) instead:

var a = 10;
console.log(a)

但如果您的真实代码比这更复杂,那么你将会要么必须手动连接,例如:

But if your real code is more complicated than that, you'll either have to concatenate manually, eg:

`foo${somevar}bar${somevar2}baz`

更改为

'foo' + somevar + 'bar' + somevar2 + 'baz'

或者,更好的选择,如果您喜欢模板文字的语法并且不喜欢纯字符串连接,那么将Babel集成到您的构建过程中以将ES6 +语法(包括模板文字)自动转换为ES5

Or, a better option, if you like the syntax of template literals and don't like plain string concatenation, would be to integrate Babel into your build process to transpile ES6+ syntax (including template literals) to ES5 automatically:

https://babeljs.io/repl/

(Babel将不仅是转换模板文字,它还会将新的语法所有转换为ES5,如解构,箭头函数, async / await 等 - 对于更大的代码库,它非常重要,允许程序员使用最新最好的语言版本,同时保持与IE等古老浏览器兼容)

(Babel will not only transpile template literals, it'll transpile pretty much all of the newer syntax to ES5, like destructuring, arrow functions, async/await, etc - for larger codebases, it's pretty much essential, allowing the programmers to write in the latest and greatest version of the language while remaining compatible with ancient browsers like IE)

这篇关于在除IE之外的其他浏览器上工作的模板文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆