[英]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()
在imports
的app.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.