我在AngularJS中使用Yii2。我必须致电不同的页面,例如关于我们的页面,联系我们等。但是我的漂亮网址无法正常工作。我需要这样的PrettyUrl:
localhost/angularjsyii/frontend/web/site/#/login
目前无法使用的网址:localhost/angularjsyii/frontend/web/site#!/#%2Flogin
.htaccess

RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php

main.php(<navbar>)
   <?php
use frontend\assets\AppAsset;

/* @var $this \yii\web\View */

AppAsset::register($this);
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>" ng-app="app">
<head>
<meta charset="<?= Yii::$app->charset ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My Angular Yii Application</title>
<?php $this->head() ?>

<script>paceOptions = {ajax: {trackMethods: ['GET', 'POST']}};</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/pace.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/themes/red/pace-theme-minimal.css" rel="stylesheet" />
<a base href="/angularjsyii/frontend/web/"></a>
</head>
<body>
<?php $this->beginBody() ?>
<div class="wrap">
    <nav class="navbar-inverse navbar-fixed-top navbar" role="navigation" bs-navbar>
        <div class="container">
            <div class="navbar-header">
                <button ng-init="navCollapsed = true" ng-click="navCollapsed = !navCollapsed" type="button" class="navbar-toggle">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span></button>
                <a class="navbar-brand" href="/angularjsyii/frontend/web/site/index">My Company</a>
            </div>
            <div ng-class="!navCollapsed && 'in'" ng-click="navCollapsed=true" class="collapse navbar-collapse" >
                <ul class="navbar-nav navbar-right nav">
                    <li data-match-route="/">
                        <a href="/">Home</a>
                    </li>
                    <li data-match-route="/about">
                        <a href="/about">About</a>
                    </li>
                    <li data-match-route="/contact">
                        <a href="/contact">Contact</a>
                    </li>
                    <li data-match-route="/login">
                        <a href="/login">Login</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container">
        <div ng-view>
        </div>
    </div>

</div>

<footer class="footer">
    <div class="container">

        <p class="pull-right"><?= Yii::powered() ?> <?= Yii::getVersion() ?></p>
    </div>
</footer>

<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>

app.js(用于路由)
    'use strict';

    var app = angular.module('app', [
        'ngRoute',      //$routeProvider
        'mgcrea.ngStrap'//bs-navbar, data-match-route directives
    ]);

    app.config(['$routeProvider',
        function($routeProvider) {
            $routeProvider.
                when('/', {
                    templateUrl: 'partials/index.html'
                }).
                when('/about', {
                    templateUrl: 'partials/about.html'
                }).
                when('/contact', {
                    templateUrl: 'partials/contact.html'
                }).
                when('/login', {
                    templateUrl: 'partials/login.html'
                }).
                otherwise({
                    templateUrl: 'partials/404.html'
                });
        }
    ])

;

config / main.php
'urlManager' => [
    'class' => 'yii\web\UrlManager',
    // Disable index.php
    'showScriptName' => false,
    // Disable r= routes
    'enablePrettyUrl' => true,
],

最佳答案

您应该在AngularJS中启用HTML5模式,以禁用路由中的hashPrefix !

app.config(['$routeProvider', '$locationProvider',
    function($routeProvider, $locationProvider) {
        $locationProvider.html5Mode(true);
        $routeProvider.
        when('/', {
            templateUrl: 'partials/index.html'
        }).
        when('/about', {
            templateUrl: 'partials/about.html'
        }).
        when('/contact', {
            templateUrl: 'partials/contact.html'
        }).
        when('/login', {
            templateUrl: 'partials/login.html'
        }).
        otherwise({
            templateUrl: 'partials/404.html'
        });
    }
]);

通过阅读AngularJS的$location documentation:



在您的情况下,基本href应该类似于<a base href="/angularjsyii/frontend/web/site">。现在,您不再需要URL上的#。更改路线,例如:
<li data-match-route="/login">
    <a href="/login">Login</a>
</li>

该URL应该最终看起来像:localhost/angularjsyii/frontend/web/site/login,它更漂亮。

07-26 05:38