简体   繁体   English

带有咕unt声的html5模式无法正常工作

[英]html5 mode with grunt serve not working

In grunt file, livereload block looks like this: 在grunt文件中,livereload块如下所示:

livereload: {
  options: {
    open: true,
    middleware: function(connect, options, middleware) {
      var optBase = (typeof options.base === 'string') ? [options.base] : options.base;
      return [
        [require('connect-modrewrite')(['!(\\..+)$ / [L]'])].concat(
          optBase.map(function(path) {
            return connect.static(path);
          })),
        connect.static('.tmp'),
        connect().use(
          '/bower_components',
          connect.static('./bower_components')
        ),
        connect().use(
          '/app/styles',
          connect.static('./app/styles')
        ),
        connect.static(appConfig.app)
      ];
    }
  }
},

Adding: 新增:

 [require('connect-modrewrite')(['!(\\..+)$ / [L]'])].concat(
                    optBase.map(function(path){ return connect.static(path); })),

did use to work for me to enable html5 mode, otherwise, my routes do not load without #! 确实为我启用了html5模式,否则我的路由将在没有#的情况下加载! when I try to reload via the browser. 当我尝试通过浏览器重新加载时。

I do have base href='/' added and html5Mode(true) in config. 我确实在配置中添加了基本href ='/'和html5Mode(true)。 Is there anything else I can try? 还有什么我可以尝试的吗? Why would it really stop working? 为什么它真的停止工作?

Note: Turns out that my URL has a dot in it and that is not being handled by connect-mod rewrite rule so well. 注意:事实证明,我的URL中有一个点,并且connect-mod重写规则无法很好地对其进行处理。 Any idea how to change that and enable it to handle dot in URL? 知道如何更改它并使其能够处理URL中的点吗?

Try adding this in root app module in the config block. 尝试将其添加到config块中的root应用模块中。 Make sure to include/inject $locationProvider. 确保包括/注入$ locationProvider。

$locationProvider
  .html5Mode({
    enabled: true,
    requireBase: false
  })
  .hashPrefix('!');

I believe the dot problem is a side effect of the general problem. 我相信点问题是普遍问题的副作用。 Have the same setup, more or less. 或多或少具有相同的设置。 I simply reset the html5Mode in .config() like this : 我只是像这样在.config()重置html5Mode

if (window.location.host == 'localhost:9000') {
   $locationProvider.html5Mode(false);
   $locationProvider.hashPrefix('');
} else {
   $locationProvider.html5Mode(true);
}

The app will still prefix all urls with #/ in development mode, making urls, linking and livereload a pain. 该应用程序仍将在开发模式下为所有URL加上#/前缀,这使URL,链接和livereload变得麻烦。 Solved this by a small directive that changes regular /urls to /#/urls : 通过一个小的指令将常规/urls更改为/#/urls来解决此问题:

var isLocalHost = (location.hostname === "localhost" || location.hostname === "127.0.0.1");
angular.module('myApp').directive('debugLink', function($timeout) {
  return {
    restrict: 'A',
    link: function(scope, element, attrs) {
      $timeout(function() {         
        var href = element.attr('href');
        if (href && isLocalHost && href.charAt(0) == '/') {
          element.attr('href', '#'+href);
        }
      }, 100);
    }
  }
});

Now linking can be done by <a href="/url" debug-link>link</a> 现在可以通过<a href="/url" debug-link>link</a>完成<a href="/url" debug-link>link</a>

Finally I use the .htaccess redirect from https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode on the production server 最后,我使用https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode上的.htaccess重定向生产服务器

RewriteEngine on

# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]

I use this approach in several angularjs+grunt projects. 我在多个angularjs + grunt项目中使用了这种方法。 It prevent the livereload from breaking, ie when I hit Ctrl - S the page is refreshed, and I can use the correct urls anywhere. 它可以防止livereload中断,即,当我按Ctrl - S时 ,页面会刷新,并且我可以在任何地方使用正确的URL。 I guess no one is interested in having prefixes or hashes in production mode. 我想没有人会对在生产模式下使用前缀或哈希值感兴趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM