gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 00:21:34 24 4
gpt4 key购买 nike

尝试学习NodeJS的基本功能,卡在append String to file with variable:

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

var user = os.userInfo();

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

从 test.txt 我得到:

Hello ${user.username}!

这里出了什么问题?

最佳答案

您需要使用反引号 (`) 而不是 '

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.

关于javascript - 字符串中的 NodeJS v6.9.1 变量不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40901840/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com