ui-router not producing correct href attribute in

2019-06-10 01:30发布

问题:

$stateProvider
  .state('admin', {
    abstract: true,
    url: '/admin',
    template: '<div ui-view></div>'
  })


<a ui-sref="admin">admin</a>

The url I get is <a href="/admin">admin</a> when it should be /#/admin -- this breaks when I command click on a link to open in a new tab.

回答1:

This is a known issue, reported as a bug. Will be fixed in next release. Here is a workaround:

Impossible to disable html5Mode with angular 1.3.0-rc.3 #1397

Due to:

angular/angular.js@dc3de7f

the html5mode-check in urlRouter.js [line 383] is no longer correct. And thus it's impossible to disable html5mode.

A quick fix could be:

var isHtml5 = $locationProvider.html5Mode();
if (angular.isObject(isHtml5)) {
  isHtml5 = isHtml5.enabled;
}

Also there is suggestion by Chris T (UI-Router team member) - see comments under this Q & A

Use the pre-release version 0.2.12-pre1 for now. bit.ly/UIR-0212pre1 ... it has a few fixes for 1.3.0 compat. – Chris T Oct 16 at 16:13



回答2:

Try to set the html5Mode in $locationProvider:

$locationProvider.html5Mode(true);

Also, I found this issue, it may be related (I don't know which version you are using):

Impossible to disable html5Mode with angular 1.3.0-rc.3