【问题标题】:Grunt browserSync grunt-php does not reload PHP files on changeGrunt browserSync grunt-php 不会在更改时重新加载 PHP 文件
【发布时间】:2016-10-28 06:21:51
【问题描述】:

我正在使用 Grunt + browserSync + grunt-php。服务器正常启动。问题是每当我对 PHP 文件进行更改时,这些更改不会在浏览器中自动重新加载。尽管设置到位,但我必须手动重新加载页面。过去 1 周一直在尝试解决此问题,但没有成功。尝试了其他在线资源,但也没有帮助。请帮忙。

目录结构:

my_app/
   src/
      index.php
      about.php
   dist/

Gruntfile.js:

"use strict";

module.exports = function (grunt) {

    grunt.initConfig({

        pkg: grunt.file.readJSON('package.json'),

        watch: {
            php: {
                files: ['src/**/*.php']
            }
        },

        browserSync: {
            dev: {
                bsFiles: {
                    src: 'src/**/*.php'
                },
                options: {
                    proxy: '127.0.0.1:8010', //our PHP server
                    port: 8080, // our new port
                    open: true,
                    watchTask: true
                }
            }
        },

        php: {
            dev: {
                options: {
                    port: 8010,
                    base: 'src'
                }
            }
        }

    });


    grunt.registerTask('default', [
        'php', // Using the PHP instance as a proxy
        'browserSync',
        'watch'             // Any other watch tasks you want to run
    ]);

};

【问题讨论】:

    标签: grunt-contrib-watch gruntfile grunt-php grunt-browser-sync


    【解决方案1】:

    一个善良的灵魂帮助我回答了这个问题。我不相信答案,并想分享解决方案,以便它可以帮助有需要的人。这里是:

    1) 只需确保您要重新加载的 PHP 文件中有 body 标签。

    2) 在页面中包含以下JS代码:

    <script id="__bs_script__">
    //<![CDATA[
        document.write("<script async src='/browser-sync/browser-sync-client.js?v=2.17.5'><\/script>".replace("HOST", location.hostname));
    //]]>
    </script>
    

    【讨论】:

      猜你喜欢
      • 2020-04-06
      • 2018-09-04
      • 1970-01-01
      • 2015-06-25
      • 1970-01-01
      • 1970-01-01
      • 2019-08-16
      • 2014-01-21
      • 1970-01-01
      相关资源
      最近更新 更多