UIRouterConfig | UI-Router
Options
All
  • Public
  • Public/Protected
  • All
Menu

Class UIRouterConfig

Provides states configuration to UI-Router during application bootstrap.

An instance of this class should be provide()d to the application bootstrap().

example
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.

example

// 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);
}

Hierarchy

  • UIRouterConfig

Index

Methods

Methods

  • configure(uiRouter: UIRouter): void
  • 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.

    Parameters

    • uiRouter: UIRouter
      :

      the uiRouter instance being configured

    Returns void


Generated using TypeDoc