我是新手,这是代码:
const gulp = require('gulp');
let tasks = [
['html'],
['src/*.html']
];
gulp.task('html', async () => {
gulp.src('src/*.html')
.pipe(gulp.dest('dist'));
})
let currentSrc = tasks[1, 0]
let currentTask = tasks[0, 0]
gulp.task('watch', async () => {
// no error but doesn't work:
gulp.watch(currentSrc.toString(), gulp.series('html'));
// this works instead:
// gulp.watch('src/*.html', gulp.series('html'));
})
我想使参数更具可读性,但不能替换任何参数。字符串是相同的,都是typeof字符串,也tasks[1][0] === 'src/*.html'
如果它们相同,为什么不能互换?
当我gulp watch
在终端中使用时,它不会给我一个错误,但是它也不起作用。我检查了所有其他内容,由于tasks[1][0] !== 'src/*.html'
某种原因,问题是100%。我只剩下几根头发了:(
解决:我的最终摘要
const gulp = require('gulp');
const tasks = [
['html'],
['src/*.html']
];
gulp.task('html', async () => {
gulp.src('src/*.html')
.pipe(gulp.dest('dist'));
})
let currentSrc = tasks[1][0].toString();
let currentTask = gulp.series(tasks[0][0])
gulp.task('watch', async () => {
gulp.watch(currentSrc, currentTask);
})
您已经tasks[1, 0]
在代码中了。它必须是tasks[1][0]
。
let tasks = [
['html', 'imagemin', 'minify'],
['src/*.html', 'src/images/*', 'src/javascript/*']
];
console.log(tasks[1, 0])
您没有进行基本检查来验证您的假设。运行该代码段,该代码段来自问题中的代码。
然后运行这个:
const tasks = [
['html', 'imagemin', 'minify'],
['src/*.html', 'src/images/*', 'src/javascript/*']
];
console.log(tasks[1][0])
如果仅将其简化为“最小可复制示例”,则将发现此错误。80%的时间,您会发现问题所在。
另外,请使用const
。
重新阅读您的问题,看起来好像是您的代码中没有看到的错字。如果您盯着它看半天,那将会发生!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句