首页 > 解决方案 > 为了做“标记模板”,我不能传递一个返回模板文字作为参数的变量,这是真的吗?

问题描述

这有效:

hero = "batman";
sidekick = "robin";
villain = "the joker"

function f(strings, good, helper, bad) {
    console.log(strings); //expected: " and must team up to defeat "
    console.log(good); //expect: "batman"
    console.log(helper);//expect: "robin"
    console.log(bad);//expect: "the joker"
}

f`${hero} and ${sidekick} must team up to defeat ${villain}`;

但是,当我将模板文字存储在变量中并尝试将变量传递给函数时,它不起作用:

hero = "batman";
sidekick = "robin";
villain = "the joker"

sentence = `${hero} and ${sidekick} must team up to defeat ${villain}`;

function f(strings, good, helper, bad) {
    console.log(strings); //expected: " and must team up to defeat "
    console.log(good); //expect: "batman"
    console.log(helper);//expect: "robin"
    console.log(bad);//expect: "the joker"
}

fsentence;

有解决办法吗?

标签: javascript

解决方案


推荐阅读