【问题标题】:How to use `otherwise` in ui-router of angular 2?如何在 Angular 2 的 ui-router 中使用“otherwise”?
【发布时间】:2016-10-26 03:36:33
【问题描述】:

在 ui-router 中我们可以添加otherwise 来处理无效/未注册状态。这是在 AngularJS 中

$urlRouterProvider.otherwise('/index');

意思是,每个无效的 URL 最终都会转到'/index'。我们如何在 Angular 2 的 ui-router 中做同样的事情?我已经在importsapp.module.ts 中使用了UIRouterModule.forRoot()UIRouterModule.forChild()

UIRouterModule.forRoot({ states: [loginState] }),
UIRouterModule.forChild({ states: [homeState] })

【问题讨论】:

    标签: javascript angularjs angular angular-ui-router


    【解决方案1】:

    UIRouterModule.forRoot docs 显示了如何使用otherwise 属性的示例:

    UIRouterModule.forRoot({ states: [loginState], otherwise: '/index' }),
    UIRouterModule.forChild({ states: [homeState] })
    

    【讨论】:

    • 就是这样!感谢您的帮助!
    【解决方案2】:

    您实际上将 $urlRouterProvider 与 $stateProvider 结合使用

    所以,首先你需要用 $stateProvider 定义你的状态

     $stateProvider
        .state("otherwise", { url : '/index'...})
    

    然后将$urlRouterProvider上的.otherwise设置为我们首先定义的状态的url

    $urlRouterProvider
            .otherwise('/index');
    

    【讨论】:

      猜你喜欢
      • 2015-01-02
      • 1970-01-01
      • 2017-06-11
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      相关资源
      最近更新 更多