log(`${chalk.magenta(' LAUNCH_COMMAND')} ${chalk.green('npm run: ')} ${chalk.red('${LAUNCH_COMMAND}')}` );
这是问题部分:${chalk.red('${LAUNCH_COMMAND}')}
LAUNCH_COMMAND
是“生产”或“开发”。然而它在另一个里面${}
。
log(`${chalk.magenta(' LAUNCH_COMMAND')} ${chalk.green('npm run: ')} ${chalk.red('${LAUNCH_COMMAND}')}` );
这是问题部分:${chalk.red('${LAUNCH_COMMAND}')}
LAUNCH_COMMAND
是“生产”或“开发”。然而它在另一个里面${}
。
只需在模板字符串文字中使用嵌套变量的变量名称
`${chalk.red(LAUNCH_COMMAND)}` // for nested sting literal just use the variable name
const LAUNCH_COMMAND = 'hi';
console.log(`${chalk.magenta(' LAUNCH_COMMAND')} ${chalk.green('npm run: ')} ${chalk.red(LAUNCH_COMMAND)}` );
您不要用引号将变量包装在字符串文字中。
log(`${chalk.magenta( LAUNCH_COMMAND)} ${chalk.green(npm run: )} ${chalk.red(LAUNCH_COMMAND})` );
^ 应该假设有效,尽管我不清楚您为什么要编写这样的字符串背后的上下文。我假设它是某种动态输出。
var b = "Cats"
var c = "Dogs"
function concat(one, two) {
return `${one} and ${two}`;
}
function compare(one, two) {
var ans = one == two ? 'Are the same' : 'Are not the same';
return ans;
}
console.log(`${concat(b, c)} - ${compare(b, c)}`);