AngularJS:如何从URL中删除#!/(bang前缀)?

Vol*_*il3 8 routing angularjs angular-ui-router angularjs-1.6

我已经做了$locationProvider.html5Mode(true);但是没有用.每当我访问http://example.com它去http://example.com/#!/.代码在这里给出:

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

myApp.config(['$qProvider', function ($qProvider) {
    $qProvider.errorOnUnhandledRejections(false);
}]);

myApp.config(function($stateProvider, $urlRouterProvider,$locationProvider) {
    // For any unmatched url, redirect to EXTRANET home page
    $urlRouterProvider.otherwise('/');
    // Now set up the states
    $stateProvider
    .state('listrole', {
    url: "/list-role",
    views: {
      'main-content': {
        templateUrl: rootUrl+ 'views/main.html',
        controller: 'rolesCtrl'
      }
    }
    })
    $locationProvider.hashPrefix('');
    $locationProvider.html5Mode(true);
});
Run Code Online (Sandbox Code Playgroud)

更新 我添加$locationProvider.hashPrefix('');,但没有区别.另外,让我告诉你,我在地址栏中输入地址并点击进入.我做对了吗?浏览器如何发现它不需要从服务器刷新?

更新#2

理想的情况是我想要的网址为http://example.comhttp://example.com/#!/list-rolehttp://example.com/list-role.现在http://example.com/list-role给404

更新#3

我正在使用nginx代理,如果这有帮助.

更新#4

删除缓存.现在我可以看到http://example.com而不是http://example.com/#!但仍然http://example.com/list-role给404

Mis*_*lis 20

如果要删除此前缀,请将此代码添加到配置中:

appModule.config(['$locationProvider', function($locationProvider) {
  $locationProvider.hashPrefix('');
}]);
Run Code Online (Sandbox Code Playgroud)

来源此处获取更多信息.


更新

如果要删除整个前缀(#而不仅仅是!),可以尝试以下解决方案:

1)激活HTML5模式并删除!模块配置中的前缀

$locationProvider.html5Mode(true);
$locationProvider.hashPrefix('');
Run Code Online (Sandbox Code Playgroud)

2)然后/<head>index.html上设置base

<head>
    ...
    <base href="/">
</head>
Run Code Online (Sandbox Code Playgroud)

  • 刷新它不工作..我刷新时得到404, (7认同)
  • 你所说的删除整个前缀(#而且不仅仅是!)的方式对我不起作用 (2认同)