【问题标题】:Angular UI-Router, not found on URL refreshing [duplicate]Angular UI-Router,在 URL 刷新时找不到 [重复]
【发布时间】:2017-06-24 04:02:37
【问题描述】:

当我输入http://localhost:6060/home时,它显示我无法获取/home,但是当我单击按钮$state.go('home')时,它显示,并且URL仍然是http://localhost:6060/home。但是当我刷新时,它仍然没有显示。输入网址时如何显示?

这是我的代码:

路由器.js

$stateProvider.state("home", {
    url: "/home",
    template: "<h2>I am a router test</h2>"
});
$locationProvider.html5Mode(true);

index.html

<head>
    <title>for test</title>
    <base href="/">
</head>
<body ng-app="my.module">
    <h1>I am just a test</h1>
    <div ng-controller="test"><button class="btn btn-default" 
         ng-click="testclick()">click me</button></div>
    <ui-view></ui-view>
</body>

控制器:

$scope.testclick = function () {
   $state.go('home');
};

如果我不使用$locationProvider.html5Mode(true); 并且没有设置base 标签,我必须输入localhost:6060/#!/home! 是什么意思?

【问题讨论】:

标签: javascript angularjs angular-ui-router


【解决方案1】:

您有服务器端(PHP、Node.js 等)吗?要使用html5Mode(true),您需要服务器端配置,它将所有路由转发到索引页面,因此您可以刷新页面而不会出现“无法获取”错误。


是什么!什么意思?

要理解这一点,您应该阅读 Angular documentation:感叹号字符 ! 是 hashbang 模式,它是默认 URL 格式,也是旧版浏览器中 HTML5 模式的后备。

【讨论】:

    猜你喜欢
    • 2015-06-09
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多