使用AngularJSHTML 5模式重新加载页面会产生错误的GET请求

使用AngularJSHTML 5模式重新加载页面会产生错误的GET请求

我想为我的应用程序启用HTML 5模式。我已经为配置放置了以下代码,如下所示这里:

return app.config(['$routeProvider','$locationProvider', function($routeProvider,$locationProvider) {

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

    $routeProvider.when('/', {
        templateUrl: '/views/index.html',
        controller: 'indexCtrl'
    });
    $routeProvider.when('/about',{
        templateUrl: '/views/about.html',
        controller: 'AboutCtrl'
    });

如你所见,我用了$locationProvider.html5mode我更改了我在ng-href排除/#/.

问题

现在,我可以去localhost:9000/并查看索引页并导航到其他页面,如localhost:9000/about.

但是,当我刷新localhost:9000/about一页。我得到以下输出:Cannot GET /about

如果我看一下网络呼叫:

Request URL:localhost:9000/aboutRequest Method:GET

如果我第一次去localhost:9000/然后单击导航到/about我得到:

Request URL:http://localhost:9000/views/about.html

它完美地呈现了页面。

当我刷新时,如何启用角获取正确的页面?


一只斗牛犬
浏览 645回答 3
3回答

慕桂英3389331

我也遇到了类似的问题,我通过以下方式解决了这个问题:使用<base href="/index.html">在索引页中在我的节点/快速服务器中使用CATCH所有路由中间件,如下所示(将其放在路由器后面):app.use(function(req,&nbsp;res)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;res.sendfile(__dirname&nbsp;+&nbsp;'/Public/index.html');});我觉得这应该能让你振作起来。如果使用Apache服务器,则可能需要重写链接。这并不难做到。只是配置文件中的一些更改。所有这些都是假设您在angularjs上启用了html 5模式。现在。请注意,在角1.2中,实际上不建议声明一个基url。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

AngularJS
Html5