2

log(`${chalk.magenta(' LAUNCH_COMMAND')} ${chalk.green('npm run: ')} ${chalk.red('${LAUNCH_COMMAND}')}` );

这是问题部分:${chalk.red('${LAUNCH_COMMAND}')}

LAUNCH_COMMAND是“生产”或“开发”。然而它在另一个里面${}

在此处输入图像描述

4

2 回答 2

3

Demo

只需在模板字符串文字中使用嵌套变量的变量名称

`${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)}` );

在此处输入图像描述

于 2017-07-14T18:55:52.627 回答
-1

您不要用引号将变量包装在字符串文字中。

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)}`);

于 2017-07-14T18:59:56.133 回答