使用 Gulp 更改后浏览器同步不刷新页面 [英] browser-sync does not refresh page after changes with Gulp

查看:30
本文介绍了使用 Gulp 更改后浏览器同步不刷新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Gulp 的新手,我想利用它的自动 scss 编译和浏览器同步.但我无法让它工作.

I'm new to Gulp and I wanted to make use of its automatic scss compiling and browser sync. But I can't get it to work.

我剥离了所有内容,只在 Browsersync 网站上保留了示例的内容:

I stripped everything down to leave only the contents of the example on the Browsersync website:

http://www.browsersync.io/docs/gulp/#gulp-sass-css

var gulp        = require('gulp');
var browserSync = require('browser-sync').create();
var sass        = require('gulp-sass');

// Static Server + watching scss/html files
gulp.task('serve', ['sass'], function() {

    browserSync.init({
        server: "./app"
    });

    gulp.watch("app/scss/*.scss", ['sass']);
    gulp.watch("app/*.html").on('change', browserSync.reload);
});

// Compile sass into CSS & auto-inject into browsers
gulp.task('sass', function() {
    return gulp.src("app/scss/*.scss")
        .pipe(sass())
        .pipe(gulp.dest("app/css"))
        .pipe(browserSync.stream());
});

gulp.task('default', ['serve']);

我可以调用 gulp serve.该网站正在显示,我从 Browsersync 收到一条消息.当我修改 HTML 时,页面会重新加载.但是,当我修改 scss 时,我可以看到:

I can call gulp serve. The site is showing and I get a message from Browsersync. When I modify the HTML, the page is reloaded. When however I modify the scss, I can see this:

[BS] 1 file changed (test.css)
[15:59:13] Finished 'sass' after 18 ms

但我必须手动重新加载.我错过了什么?

but I have to reload manually. What am I missing?

推荐答案

如果您愿意,您可以只注入更改,而不必在 SASS 编译时强制完全浏览器刷新.

You can just inject the changes instead of having to force a full browser refresh on SASS compile if you like.

browserSync.init({
    injectChanges: true,
    server: "./app"
});

gulp.task('sass', function() {
    return gulp.src("app/scss/*.scss")
        .pipe(sass())
        .pipe(gulp.dest("app/css"))
        .pipe(browserSync.stream({match: '**/*.css'}));
});

这篇关于使用 Gulp 更改后浏览器同步不刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆