连接模板文字时出现奇怪的错误

Getting weird error when concatenating template-literals

我正在尝试构建一个 javascript 脚本,它将我的页眉显示在我的 html 页面上,这样我就不必在进行更改时更新我的​​每个页面。我了解到模板文字可以在变量中保存 html 代码,所以我开始使用它。但是,当我使用三元运算符时,我在 VS Code 中弹出错误。它说我缺少一个 ).

我已经检查了整个模板文字是否缺少括号。

var html = `<div id="branding">
    <h1><span class="btop">JoJo</span> <span class="bbottom">Studios</span></h1>
</div>
<nav>
    <ul>
        <li`+(page=="home" ? ` class="current><a href="#">Home</a>"` : `><a href="../">Home</a>`)+`</li>
        <li`+(page=="games" ? ` class="current"` : ``)+`>
            <div class="dropdown">
                <a href=`+(page=="games" ? `"./"` : (page=="home" ? `"games/"` : `"../games/"`)+`>Games</a>
                <div class="dropdown-content">
                    <a href=`+(page=="games" ? `"` : (page=="home" ? `"games/` : `"../games/`)+`jojobananacatch.html">JoJo Banana Catch</a>
                </div>
            </div>
        </li>
        <li`+(page=="play" ? ` class="current"` : ``)+`><a href=`+(page=="home" ? `"play/"` : `"../play/"`)+`>Play</a></li>
        <li`+(page=="videos" ? ` class="current"` : ``)+`><a href=`+(page=="home" ? `"videos/"` : `"../videos/"`)+`>DevLogs & More</a></li>
        <li`+(page=="about" ? ` class="current"` : ``)+`><a href=`+(page=="home" ? `"about/"` : `"../about/"`)+`>About</a></li>
        <li`+(page=="contact" ? ` class="current"` : ``)+`><a href=`+(page=="home" ? `"contact/"` : `"../contact/"`)+`>Contact</a></li>
        <li`+(page=="account" ? ` class="current"` : ``)+`><a href=`+(page=="home" ? `"account/account.php?account_forward=0"` : `"../account/account.php?account_forward=0"`)+`>Account</a></li>
    </ul>
</nav>`;

输出应该只是一个我可以插入到文档中的字符串。任何帮助将不胜感激。

dropdown class 里面的部分是问题所在,你显然使用了两个嵌套的三元运算符,但只有一个右括号。

改变

<a href=`+(page=="games"
           ? `"./"`
           : (page=="home"
              ? `"games/"`
              : `"../games/"`)+`>Games</a>

<a href=`+(page=="games"
           ? `"./"`
           : (page=="home"
              ? `"games/"`
              : `"../games/"`))+`>Games</a>

第二个也一样

顺便说一下,您也可以在模板中直接使用表达式(这就是模板的全部意义所在)。

这是使用 ${...} 语法完成的,在某些情况下,这种语法可能比关闭并重新打开模板更容易阅读。例如:

let s = "World";
console.log(`Hello, ${s+"."}`)

日志Hello, World.

在 Javascript (ES6 >) 中,模板字符串根本不是这样工作的。

不要忘记 + 不会实际连接结果,也不会在其中包含逻辑表达式。你必须使用这个 ${...} 来包围你的 js 表达式,以便它们的结果立即转换为字符串。

例子

var html = `<li ${page=="home" ? '"class="current><a href="#">Home</a>':'<a href="../">Home</a>'}</li>`;

这就是您使用模板字符串的方式(在字符串中插入逻辑)。 还要考虑将您在三元组中返回的内容提取到它们自己的单独变量中。有助于更好地阅读。

例子

const currentPageLink = 'class="current"><a href="#">Home</a>';
const previousPageLink = '<a href="../">Home</a>';

// and then following the example above

const html = `<li ${page==="home" ? currentAnchor: previousPageLink}</li>`;

希望这对您有所帮助。

虽然这已经得到回答,(缺少嵌套三元的右圆括号)我觉得你可能会发现使用 string interpolation with template literals 的好处。通过这样做,您可以删除字符串中的所有连接(这是使用模板文字而不是常规字符串的主要优点之一)。

let page = 'play';
let html = `<div id="branding">
    <h1><span class="btop">JoJo</span> <span class="bbottom">Studios</span></h1>
</div>
<nav>
    <ul>
        <li ${(page==="home" ? `class="current><a href="#">Home</a>"` : `><a href="../">Home</a>`)}</li>
        <li ${(page==="games" ? `class="current"` : ``)}>
        <div class="dropdown">
            <a href=${(page==="games" ? `"./"` : (page==="home" ? `"games/"` : `"../games/"`))}>Games</a>
            <div class="dropdown-content">
                <a href=${(page==="games" ? `"` : (page==="home" ? `"games/` : `"../games/`))}jojobananacatch.html">JoJo Banana Catch</a>
            </div>
        </div>
        </li>
        <li ${(page==="play" ? ` class="current"` : ``)}><a href=${(page==="home" ? `"play/"` : `"../play/"`)}>Play</a></li>
        <li ${(page==="videos" ? ` class="current"` : ``)}><a href=${(page==="home" ? `"videos/"` : `"../videos/"`)}>DevLogs & More</a></li>
        <li ${(page==="about" ? ` class="current"` : ``)}><a href=${(page==="home" ? `"about/"` : `"../about/"`)}>About</a></li>
        <li ${(page==="contact" ? ` class="current"` : ``)}><a href=${(page==="home" ? `"contact/"` : `"../contact/"`)}>Contact</a></li>
        <li ${(page==="account" ? ` class="current"` : ``)}><a href=${(page==="home" ? `"account/account.php?account_forward=0"` : `"../account/account.php?account_forward=0"`)}>Account</a></li>
    </ul>
</nav>`;
console.log(html);