$locationProvider.html5Mode(true) issues

2020-03-15 05:52发布

问题:

been dealing with some $locationProvider issues that I am stuck on. I've got a simple single page page. But I'm getting the following error:

     TypeError: Cannot read property 'replace' of undefined
        at trimEmptyHash (angular.js:10551)
        at Object.$locationWatch (angular.js:11399)
        at Scope.$get.Scope.$digest (angular.js:14217)
        at Scope.$get.Scope.$apply (angular.js:14488)
        at bootstrapApply (angular.js:1449)
        at Object.invoke (angular.js:4182)
        at doBootstrap (angular.js:1447)
        at bootstrap (angular.js:1467)
        at angularInit (angular.js:1361)
        at HTMLDocument.<anonymous> (angular.js:26065)

My app.js file is pretty simple...

    var app = angular.module('app',
        [
            'ui.router'
        ]
    );

    app.config([
        '$stateProvider',
        '$httpProvider',
        '$locationProvider',
        '$urlRouterProvider',
        function ($stateProvider, $httpProvider, $locationProvider, $urlRouterProvider) {

            $locationProvider.hashPrefix('!');
            $locationProvider.html5Mode(true);

            $stateProvider
            .state('home',
            {
                url: '/',
                views: {
                    'aboutView':
                        {
                            template: function (params) {
                                console.log("home");
                                return 'home';
                            }
                        }
                }
            })
            .state('about',
            {
                url: '/about',
                views: {
                    'aboutView':
                        {
                            template: function (params) {
                                console.log('about')
                                return 'about';
                            }
                        }
                }
            })
            ;
        }
    ]);

I do have the <base href="http://localhost/apps/uiv8/" /> set in my index.html file. So, when I comment out the $locationProvider code, everything works fine in # mode. I can get to /#/about, etc. without issue. As soon as I put the $locationProvider parts back in, nothing.

A little bit more about my environment.... We do have asp.net's MVC in here, and the route.config is doing a {*url} to redirect all to the default route, and I've even gone as far as modifying IIS with url rewrites to send to the default, but I still get the parse Error above.

So, anybody got any ideas what's going on?

Thanks, Nick

回答1:

Add the base tag in your index page like :

<base href="/">

Instead of :

<base href="http://localhost/apps/uiv8/" />

Hope it will solve your problem.



回答2:

Make sure your Application Name in IIS is the same as what is in your base tag

<base href='http://localhost/apps/uiv10'>

so ensure your IIS name is app/uiv10

or whatever combo of case you want, but they need to be the same.