我已经创建了一个类来处理我的通知。为了改变文本颜色,我使用的包接受以下内容:
const chalk = require('chalk');
chalk`{red This text will be red.}`;然而,我现在已经将这个字符串模板传递到一个方法中,然后该方法将其传递给粉笔,然而粉笔包并没有解析字符串模板。因此,日志不是改变颜色,而是显示传入的字符串。
const log = require('./gulp-includes/log');
let test = 'helloworld';
log.all({
message: `{red This text will be read. ${test}}`
});gulp-include/log.js
const settings = require('./settings.js');
const chalk = require('chalk');
const log = require('fancy-log');
const notifier = require('node-notifier');
class Log
{
all(params) {
this.log(params);
}
log(params) {
log(chalk`${params.message}`);
}
}
module.exports = new Log();我怎样才能解决这个问题?
发布于 2019-06-10 16:14:25
要在chalk类中生成Log解析字符串模板,需要手动模拟标记模板文字 --自己编写标记函数。
幸运的是,在这种情况下,像字符串模板中的${test}这样的表达式在第一次出现时已经被计算过了。因此,传递给chalk的唯一参数是半解析字符串,例如'{red This text will be read. helloworld}' (${params.message}的值),这使事情变得更简单。
在Log类中,可以通过以下方法模拟chalk标记的模板文字:
log(params) {
let message = params.message;
let options = [message];
options.raw = [message];
log(chalk(options));
}https://stackoverflow.com/questions/56526522
复制相似问题