【问题标题】:ui routing without urlRouterProvider.otherwise没有 urlRouterProvider.otherwise 的 ui 路由
【发布时间】:2016-03-15 10:49:57
【问题描述】:

如果我不包含 $urlRouterProvider.otherwise('/'); 则不会发生路由在配置中。

我的 go 应用程序引擎设置与 gorilla 是

r := mux.NewRouter()
r.HandleFunc(/admin, handleAdmin)

和 angularjs 配置是;

angular.module('admin')
    .config(function($stateProvider, $urlRouterProvider) {
      $stateProvider.state('home', {
        url: '/',
        templateUrl: '/admin/index.html',
        controller: 'AdminController as admin'
      });
  //$urlRouterProvider.otherwise('/');
})

当我不调用 $urlRouterProvider.othwerwise 行时,我打开 http://localhost:8080/admin 我希望看到 admin/index.html,但我没有。只有当我手动导航到http://localhost:8080/admin#/ 时,我才会看到它。 但是如果我添加 $urlRouterProvider.othwerwise 选项并转到 http://localhost:8080/admin 它会自动重定向到 http://localhost:8080/admin#/ 我认为这不是通常的做法,因为我可能希望“否则”路由到自定义 404 页面。我错过了什么?

【问题讨论】:

    标签: angularjs go routing angular-ui-router gorilla


    【解决方案1】:

    默认情况下,Angular 在 url 前面添加 hashPrefix。因此,当您导航到 http://localhost:8080/admin 时,您看不到 index.html,因为您尚未访问 angular 的 ui-router 中定义的 url。您必须导航到 http://localhost:8080/admin/#/ 才能实际处于应用程序的 / 状态。

    同样的原因,您的应用在没有.otherwise() 的情况下无法运行,因为之后它会自动将您重定向到/ 状态。

    对于可能的修复:

    在您的 .config 函数中:

    // This is in your app module config.
    $locationProvider.html5Mode(true);
    

    在你的index.html:

    // This is in your index.html head.
    <base href="/" />
    

    【讨论】:

    • 好的,我试过了,它可以工作(尽管控制台窗口中有很多错误)但是当我转到另一个模块负责的 localhost 并且我使用 ngNewRouter 时,我想要相同的功能,它会自动重定向到localhost:8080/#,而没有启用html5。
    • 您的应用程序必须有一个主模块,所有其他模块都在其中注入,将其附加到该模块的配置函数以使其在整个应用程序中运行。
    【解决方案2】:

    问题不在于没有声明 otherwise

    问题在于您的路线。您将 url 指定为 '/',这意味着状态 home 只能通过 http://localhost:8080/admin/ 而不是通过 http://localhost:8080/admin 访问>

    otherwise 所做的是。当您访问 url http://localhost:8080/admin 时,路由器会尝试找到与该 url 匹配的状态,但没有找到。因此它会重定向到与您的 home 状态匹配的 http://localhost:8080/admin/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多