Configures UI-Router before bootstrap
Configures UI-Router before bootstrap
An app should perform UI-Router configuration here, such as registering the initial set of states, parameter types, defining global hooks, etc.
the uiRouter instance being configured
Generated using TypeDoc
Provides states configuration to UI-Router during application bootstrap.
An instance of this class should be
provide()
d to the applicationbootstrap()
.import {UIROUTER_PROVIDERS, UIView} from "ui-router-ng2"; import {MyConfig} from "./app/myConfig"; bootstrap(UIView, [ ...UIROUTER_PROVIDERS, provide(UIRouterConfig, { useClass: MyConfig } ]);
The application's initial states should be registered with the UIRouter.stateRegistry. Any global configuration (transition hooks, parameter types, etc) should be done here.
// myconfig.ts import {STATES} from "./states"; import {registerAuthHook} from "./hooks"; import {registerSlugType} from "./paramtypes"; export class MyConfig { configure(uiRouter: UIRouter) { STATES.forEach(state => uiRouter.stateRegistry.register(state)); registerAuthHook(uiRouter.transitionService); registerSlugType(uiRouter.urlMatcherFactory); } } // states.ts import {FooComponent} from "./foo.component"; import {BarComponent} from "./bar.component"; import BAZ_MODULE_STATES from "./baz/states"; export let STATES = [ { name: 'foo', url: '/url', component: FooComponent}, { name: 'bar', url: '/bar', component: BarComponent} ].concat(BAZ_MODULE_STATES); // hooks.ts export function registerAuthHook(transitionService: TransitionService) { let requireAuthentication = (transition: Transition, injector: Injector) { if (!Injector.get(AuthService).isAuthenticated()) { return Injector.get(StateService).target('login'); } } transitionService.onBefore({ to: (state) => state.requiresAuth }, requireAuthentication); } // paramtypes.ts export function registerSlugType(urlMatcherFactory: UrlMatcherFactory) { let builtInStringType = urlMatcherFactory.type('string'); let slugType = Object.assign({}, builtInStringType, { encode: (str) => str, decode: (str) => str }); urlMatcherFactory.type('slug', slugType); }