1

我正在使用带有 $locationProvider.html5Mode 的 stateProvider:

    // Redirect any unmatched url
    $urlRouterProvider.otherwise("/cool");  

    $stateProvider
        // Dashboard
        .state('cool', {
            url: "/cool",
            templateUrl: "views/cool.html"
        });

     $locationProvider.html5Mode(true);

但是当我http://localhost:8080/cool会得到Cannot GET /cool错误。http://localhost:8080/并且http://localhost:8080/#/cool工作正常。


经过一些研究,我发现问题是由于 grunt connect。所以我添加了 modRoute 以将所有 url 路由到 index.html:

livereload: {
        options: {
          open: true,
          middleware: function (connect) {
            return [
              modRewrite([
                '!\\.\\w+$ /'
              ]),

但现在的问题是每个 url 像http://localhost:8080/uncoolhttp://localhost:8080/cool

4

2 回答 2

0

首先,确保你的头脑中有“基础”标签。

<base href="/">

然后确保您的“/cool”页面与您的“/”提供相同的功能。如果不是,服务器将返回 404(如预期的那样)。这对于您的所有“子页面”都必须是正确的。

为了测试是否是这种情况——创建一个带有指向“/cool”的href的链接,angular应该用a“在内部”处理这个pushState,这与仅仅“浏览”到那个页面不同。这意味着,当您单击链接时,该页面将按预期运行,但如果您重新加载(CTRL+ R),您将收到错误消息。

于 2015-12-07T15:23:13.220 回答
0

如果您使用,则必须在文档中的部分中html5mode包含<base>标签。head您可以设置requireBase为 false 并使用html5mode<base>标签。请查看角度文档ui-router 文档

于 2015-12-07T15:31:23.073 回答