简体   繁体   English

字符串中的NodeJS v6.9.1变量不起作用

[英]NodeJS v6.9.1 variable in string doesn't work

Try to learn basic functions from NodeJS, stuck in append String to file with variable: 尝试从NodeJS学习基本功能,将基本字符串附加到带有变量的文件中:

const fs = require('fs');
const os = require('os');

var user = os.userInfo();

fs.appendFile('test.txt', 'Hello ${user.username}!');

From test.txt I got: 从test.txt我得到:

Hello ${user.username}!

What goes wrong here? 这里出什么问题了?

You need to use back-tick (`) instead of ' 您需要使用反勾(`)代替'

fs.appendFile('test.txt', `Hello ${user.username}!`);

Template literals 模板文字

Template literals are enclosed by the back-tick (grave accent) character instead of double or single quotes. 模板文字用反引号(重音符号)而不是双引号或单引号引起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM