AngularJS路由+.htaces重写+路由参数=不工作



这本质上是对这个问题的重述,因为这个问题没有得到正确的回答。

使用一组基本的自解释htaccess重写

RewriteEngine on
    # Don't rewrite files or directories
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    # Rewrite everything else to index.html to allow html5 state links
    RewriteRule ^ index.html [L]

请考虑以下链接。

1. http://localhost/clouds
2. http://localhost/wind
3. http://localhost/city/Tokyo

链接1和2在页面刷新时工作正常,但链接3只能通过页面导航(单击链接(访问。如果我在浏览器中手动粘贴第三个链接,我会进入索引页面,这与链接1和2不同。

我的路线配置:

theApp.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
    "use strict";
    console.log('Configuring');
    $routeProvider.when('/clouds', {
        templateUrl: 'Views/Cloud.html',
        controller: 'cloudController'
    }).when('/wind', {
        templateUrl: 'Views/Wind.html',
        controller: 'windController'
    }).when('/city/:cityName', {
        templateUrl: 'Views/City.html',
        controller: 'cityController'
    });
    $locationProvider.html5Mode(true);
}]);

附言:我确实投了。如果我没有链接1和2就不会工作。

编辑:我的坏,我没有添加导致这些链接的锚标签。

<ul class="nav navbar-nav navbar-right">
                    <li><a href="#"> Home</a></li>
                    <li><a href="/clouds"> Cloud</a></li>
                    <li><a href="/wind"> Wind</a></li>
                    <li><a href="/city/Tokyo"> CHAT!!!</a></li>
</ul>

您必须将<base href="/" />放置在标记的最顶部。它是有效的,尽管我想知道如何以及为什么!!

学分:这个堆叠流回答

最新更新