Visual Studio中JavaScript ES6语法不生效?模板字符串无法解析求解决
Hey there! I totally get why this is confusing—let's break down what's going wrong and how to fix it quickly.
The core issue here is the type of quotes you're using. You wrapped lesson: ${lesson} in single quotes ('), but ES6 template strings (the syntax that lets you embed variables with ${}) rely on backticks (the ` character, usually found above the Tab key on your keyboard).
When you use single or double quotes, JavaScript treats everything inside as literal text—so it just prints ${lesson} instead of replacing it with the actual value of your lesson variable.
Here's the corrected code that will work as expected:
const lesson = "Javascript ES6"; window.onload = function(){ document.body.innerHTML = `lesson: ${lesson}` }
A quick bonus tip: Template strings also support multi-line strings without needing to use concatenation, which is a nice perk once you get the hang of them. Just always remember to reach for backticks when you want to embed variables or expressions directly in a string!
内容的提问来源于stack exchange,提问作者user9127206




