6

I want to fix the livereload with my angular js app. I am using yoeman ui-router with html5 push state.

What do a have to do?

4

1 回答 1

11

索引 对于搜索引擎,您必须将以下内容添加到<head>您的 index.html

<meta name="fragment" content="!">
<base href="/">

应用程序 在您的 app.js 中,您必须注入以下依赖项并添加函数。

angular
  .module('yourApp', [
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngSanitize',
    'ui.router',
  ])
  .config(function ($stateProvider, $urlRouterProvider, $locationProvider) {

  // HTML5 PUSH STATE
  $locationProvider.html5Mode(true).hashPrefix('!');

  $urlRouterProvider.otherwise('/');

  // STATES
  $stateProvider
    .state('home', {
      url: '/',
      templateUrl: 'views/home.html',
      controller: 'homeCtrl'
    });
});

中间件

这个问题的解决方案是适配co​​nnect-modrewrite中间件。

在 yeomanfolder 的控制台中使用 node packetmanager 安装中间件

npm install connect-modrewrite

然后适配 Gruntfile.js

var modRewrite = require('connect-modrewrite');


livereload: {
    options: {
      open: true,
      base: [
        '.tmp',
        '<%= yeoman.app %>'
      ],
      middleware: function (connect, options) {
        var middlewares = [];

        middlewares.push(modRewrite(['^[^\\.]*$ /index.html [L]'])); //Matches everything that does not contain a '.' (period)
        options.base.forEach(function (base) {
          middlewares.push(connect.static(base));
        });

        middlewares.push(
          connect.static('.tmp'),
          connect().use(
            '/bower_components',
            connect.static('./bower_components')
          )
        );

        return middlewares;
      }
    }
  },

现在用命令开始你的咕噜声

grunt serve
于 2014-08-20T09:22:33.183 回答