Livereload Html5 Pushstate with AngularJS, ui.Router and yeoman

前端 未结 1 1223
青春惊慌失措
青春惊慌失措 2020-12-30 14:15

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?

相关标签:
1条回答
  • 2020-12-30 14:57

    The index For the searchengines you have to add the following to the <head> of your index.html

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

    The app In your app.js you have to inject the following dependencies and add the functions.

    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'
        });
    });
    

    The middelware

    The solution to this problem is to adapt the connect-modrewrite middleware.

    Install the middleware with the node packetmanager within your console in your yeomanfolder

    npm install connect-modrewrite
    

    Then adapt the 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;
          }
        }
      },
    

    Now start your grunt with the command

    grunt serve
    
    0 讨论(0)
提交回复
热议问题