【问题标题】:Angular URL's not working but ui-sref doesAngular URL 不起作用,但 ui-sref 起作用
【发布时间】:2017-03-25 17:35:21
【问题描述】:

所以这可能是一个愚蠢的错误,但我有一个 angularJs 应用程序,并且我有 ui-router 更改状态。我可以通过ui-sref$state.go() 导航到不同的页面,但由于某种原因,如果我直接在浏览器中输入网址,我会返回错误cannot GET ...。我不知道为什么,但这里是配置代码和状态设置:

应用配置:

.config(function ( $urlRouterProvider, $locationProvider) {

$locationProvider.html5Mode(true).hashPrefix('!');
$urlRouterProvider.otherwise('/');

});

项目页面组件/控制器/状态配置:

(function(){
  'use strict';


angular.module('app')
.component("itemPage",{
templateUrl: 'app/itemPage/itemPage.html',
controller: itemPageCtrl
})
.config(['$stateProvider',function ($stateProvider) {
$stateProvider
  .state('item', {
    url: '/item',
    template: '<item-page></item-page>'
  });
}]);



function itemPageCtrl(){
var ctrl = this;


}


})();

重申一下,如果我是我网站的主页并单击一个按钮,我可以使用 ui-sref or $state.go() 进入项目页面,但如果我尝试将 url 直接放入 localhost:9000/item 或者我已经在页面并单击刷新它会给出cannot GET /item 消息。

任何帮助将不胜感激,如果需要,我可以提供更多代码。 谢谢。

【问题讨论】:

    标签: javascript html angularjs angular-ui-router angular-material


    【解决方案1】:

    您应该在服务器上进行配置,以将所有请求重定向到入口点文件(通常是您的 index.html),例如在 Apache 中使用 .htaccess 文件。

    这个问题有时已经提出;

    Reloading the page gives wrong GET request with AngularJS HTML5 mode

    在 Apache 服务器上配置:htaccess redirect for Angular routes

    在节点服务器上配置:Node.js catch-all route with redirect always renders index page with Angular regardless of url

    【讨论】:

      猜你喜欢
      • 2015-06-21
      • 1970-01-01
      • 2015-05-26
      • 1970-01-01
      • 2017-09-15
      • 2015-01-23
      • 2015-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多