javascript - 深度路由未按预期工作 ui-router

标签 javascript html angularjs angular-ui-router

这是我的路由代码,正如我在互联网上检查的那样,使用 html5Mode 和 requireBase 应该可以解决这个问题。 当我直接尝试访问 localhost/index 时,会显示 Cannot GET/login

app.config(function ($stateProvider, $urlRouterProvider, $locationProvider) {
    // Setting login page as default location for the App
    $urlRouterProvider.otherwise("/login");

    $stateProvider.state('root', {
        url: '/login',
        views: {
            "temp_app": {
                templateUrl: 'app.tpl.html'
            }
        }
    });
    $locationProvider.html5Mode({ enabled: true, requireBase: false });
});

最佳答案

这是一个网络服务器问题,虽然您没有指定哪种技术,但我偷看了您用 IIS 标记的另一个问题,因此如果您确实使用 IIS,则需要包含一些 <rewrite>您的 Web.config 中的规则。 requireBase: false这也不是正确的方法,我们需要一个基础。观察以下示例...

<head>
    <base href="/">
[...]
<小时/>
app.config(function ($stateProvider, $urlRouterProvider, $locationProvider) {

    $locationProvider.html5Mode({
        enabled: true
    });
[...]
<小时/>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Main Rule" stopProcessing="true">
                    <match url=".*"/>
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
                    </conditions>
                    <action type="Rewrite" url="/"/>
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

如果我的预感是错误的,并且您没有使用 IIS,请参阅常见 Web 服务器的重写解决方案,包括(与 IIS 一起):ApacheNginx> express - How to: Configure your server to work with html5Mode

另外,请参阅$location docs欲了解更多信息,请访问 Error: $location:nobase $location in HTML5 mode requires a tag to be present!更深入地了解为什么我们需要以这种方式设置我们的应用程序

关于javascript - 深度路由未按预期工作 ui-router,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32626818/

相关文章:

javascript - 如何自动调用 RESTful Web 服务

javascript - D3 中带有工具提示的面积图

javascript - 为什么脚本不执行?

javascript - 在 HTML 中记住用户区域设置选择的最简单方法

angularjs - Protractor 测试 ng-grid 中的特定单元格

node.js - 使用 NodeJS 从服务器下载大量图像(>2GB)

javascript - 我可以让内容出现在 Div 中吗?当光标悬停在单独的 Div 上时?

javascript - 本地图上使用javascript时,在 Bootstrap 页面中显示谷歌地图

javascript - Jssor slider 从slide0播放

javascript - ui-router $transitions 钩子(Hook)在浏览器重新加载时不会被调用