Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 只执行一次任务_Javascript_Npm_Gulp_Gulp Watch - Fatal编程技术网

Javascript 只执行一次任务

Javascript 只执行一次任务,javascript,npm,gulp,gulp-watch,Javascript,Npm,Gulp,Gulp Watch,我已经安装了GulpLivereLoad,以便在对.js文件进行更改后重新加载页面 代码如下: const gulp = require('gulp'); const livereload = require('gulp-livereload'); gulp.task('jsLiveReload', () => { gulp .src('/home/ksdmwms/Desktop/projs/others/tests/gulp_test/src/js/file.js')

我已经安装了GulpLivereLoad,以便在对.js文件进行更改后重新加载页面

代码如下:

const gulp = require('gulp');
const livereload = require('gulp-livereload');


gulp.task('jsLiveReload', () => {
  gulp
    .src('/home/ksdmwms/Desktop/projs/others/tests/gulp_test/src/js/file.js')
    .pipe(livereload());
});


gulp.task('watchJsTest', function() {
    livereload.listen({
        reloadPage: 'http://localhost/projs/others/tests/gulp_test/src/index.html'
    });
    gulp.watch('/home/ksdmwms/Desktop/projs/others/tests/gulp_test/src/js/file.js', gulp.series('jsLiveReload'));
});
因此,它在
file.js
上的监听发生了变化

当我执行时,我得到:

gulp watchJsTest
[14:05:40] Using gulpfile /opt/lampp/htdocs/projs/others/tests/gulp_test/gulpfile.js
[14:05:40] Starting 'watchJsTest'...
[14:05:46] Starting 'jsLiveReload'...
[14:05:46] /home/ksdmwms/Desktop/projs/others/tests/gulp_test/src/js/file.js reloaded.
它只在我保存第一次更改时重新加载,如果我再做一次更改,它不会重新加载

我怎样才能解决这个问题


注意:如果gulp安装在
/home/ksdmwms/Desktop/projs/others/tests/gulp\u test

你可以试试这个:

var gulp = require('gulp'),
    livereload = require('gulp-livereload');


gulp.task('jsLiveReload', function() {
  gulp.src('./src/js/**/*.js')
    .pipe(livereload());
  //.pipe(gulp.dest('')); 
});


gulp.task('watchJsTest', function() {
    livereload.listen({
        reloadPage: 'http://localhost/'
    });
    gulp.watch('./src/js/**/*.js', ['jsLiveReload']);
});
告诉我它是否有效:)
您已经试过了吗?

浏览器同步>实时重新加载

const gulp=require('gulp'))
const browserSync=require('browser-sync')。create()
吞咽任务('js',()=>{
回灌([
“文件1”,
“文件2”
])
.管道(大口目的地(“”))
.pipe(browserSync.stream())
})
吞咽任务('serve',['sass','js'],()=>{
browserSync.init({
服务器:'./src',
})
大口喝手表([
“src/sass/*.scss”,
“src/js/*.js”,
],['sass','js'])
gulp.watch('src/*.html')。on('change',browserSync.reload)
})
狼吞虎咽的任务('default',['serve']))

在serve任务中,gulp.watch

是否将gulp与node.js一起使用?gulp安装在哪里(路径)?是的,我在全局范围内使用它,gulp安装在项目的本地(--save dev),但它不起作用。不,我从来没有试过。。。这解决了以下问题:。管道(gulp.dest(“”));刚刚将此添加到jsLiveReload解决了以下问题:.pipe(gulp.dest(“”));