Reloading the page gives wrong GET request with AngularJS HTML5 mode

前端 未结 24 2921
慢半拍i
慢半拍i 2020-11-22 01:39

I want to enable HTML5 mode for my app. I have put the following code for the configuration, as shown here:

return app.config([\'$routeProvider\',\'$location         


        
24条回答
  •  梦如初夏
    2020-11-22 01:52

    I solved same problem using modRewrite.  
    AngularJS is reload page when after # changes.  
    But HTML5 mode remove # and invalid the reload.  
    So we should reload manually.
    # install connect-modrewrite
        $ sudo npm install connect-modrewrite --save
    
    # gulp/build.js
        'use strict';
        var gulp = require('gulp');
        var paths = gulp.paths;
        var util = require('util');
        var browserSync = require('browser-sync');
        var modRewrite  = require('connect-modrewrite');
        function browserSyncInit(baseDir, files, browser) {
            browser = browser === undefined ? 'default' : browser;
            var routes = null;
            if(baseDir === paths.src || (util.isArray(baseDir) && baseDir.indexOf(paths.src) !== -1)) {
                routes = {
                    '/bower_components': 'bower_components'
                };
            }
    
            browserSync.instance = browserSync.init(files, {
                startPath: '/',
                server: {
                baseDir: baseDir,
                middleware: [
                    modRewrite([
                        '!\\.\\w+$ /index.html [L]'
                    ])
                ],
                routes: routes
                },
                browser: browser
            });
        }
    

提交回复
热议问题