提示: Error: watching index.html: watch task has to be a function (optionally generated by using gulp.parallel or gulp.series)

解释:
从gulp4.0开始,watch函数的第二个参数必须是函数。
即以前的写法不再支持。
以前的这种写法
Gulp.watch(“监听的文件”,[任务名字符串])
不能再使用了,必须这样
Gulp.watch(“监听的文件”,回调函数)。
示例:
以前的这种写法:
gulp.task("copy-index",async ()=>{
gulp.src("index.html")
.pipe(gulp.dest("D:\\phpStudy\\WWW\\web1809\\taobao"));
});
gulp.task("watch-all",async ()=>{
gulp.watch("index.html",["copy-index"]);
});
不支持了,
必须这样写:
gulp.task("watch-all",async ()=>{
gulp.watch("index.html",async ()=>{
gulp.src("index.html")
.pipe(gulp.dest("D:\\phpStudy\\WWW\\web1809\\taobao"));
});
});
即把以前对应任务的回调函数写在watch函数的第二参数里。
本文解析了Gulp4.0中watch函数的更新,强调其第二个参数必须为函数,而非任务名字符串。通过具体示例对比了新旧写法,帮助开发者正确迁移代码。
329

被折叠的 条评论
为什么被折叠?



