簡體   English   中英

如何在角2的ui-router中使用`otherwise`?

[英]How to use `otherwise` in ui-router of angular 2?

在ui-router中,我們可以添加otherwise來處理無效/未注冊狀態。 這是在AngularJS中

$urlRouterProvider.otherwise('/index');

這意味着,每個無效的URL最終都會轉到'/index' 我們如何在ui-router中為Angular 2做同樣的事情? 我已經使用UIRouterModule.forRoot()UIRouterModule.forChild()importsapp.module.ts

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

UIRouterModule.forRoot文檔顯示了如何使用otherwise屬性的示例:

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

實際上,您將$ urlRouterProvider與$ stateProvider結合使用

所以,首先你需要用$ stateProvider定義你的狀態

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

然后在$ urlRouterProvider上將.otherwise設置為我們首先定義的狀態的url

$urlRouterProvider
        .otherwise('/index');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM