在URL刷新时找不到Angular UI-Router

时间:2017-02-07 02:54:13

标签: javascript angularjs angular-ui-router

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

以下是我的代码:

router.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!是什么意思?

1 个答案:

答案 0 :(得分:0)

你有服务器端(PHP,Node.js等)吗?要使用html5Mode(true),您需要服务器端配置,将所有路由转发到索引页面,因此您可以刷新页面而不必使用&#34;无法获取&#34;错误。

  

这是什么!意思?

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