admin管理员组文章数量:815076
从CLI解析打字稿中的文本
我正在使用带有打字稿的CLI并使用查询程序来执行此操作。
我有一个JSON。
const a = {
name: 'Mohan',
age: '5',
};
之后,我启动询问者提示。
Enquirer.prompt({
name: 'inputVal',
type: 'input',
message: 'Create a Text',
}).then(async response => {
const output: string = (response as any).inputVal;
console.log(output);
});
这将提示您输入值,我正在尝试将其写为答案
${a.name} has age ${a.age}
此将在控制台上打印以下内容
${a.name} has age ${a.age}
我想要console.log将其解析为
Mohan has age 5
我也尝试过评估,但是没有用。有谁知道我们该怎么做?
回答如下:您必须先convert a string to a template string
const a = {
name: 'Mohan',
age: '5',
};
const string = "${a.name} has age ${a.age}";
const templateString = eval('`' + string + '`');
console.log(templateString);
本文标签: 从CLI解析打字稿中的文本
版权声明:本文标题:从CLI解析打字稿中的文本 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/biancheng/1715195484a822303.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论