1
1
/** @module ng2 */ /** */
2
2
import { UIRouter } from "../router" ;
3
3
/**
4
- * Provides states configuration to UI-Router during application bootstrap.
4
+ * Configures UI-Router during application bootstrap.
5
5
*
6
- * An instance of this class should be `provide()`d to the application `bootstrap() `.
6
+ * UI-Router ng2 users should implement this class, and pass it to [[provideUIRouter]] in the root app `NgModule `.
7
7
*
8
8
* @example
9
9
* ```js
10
- * import {UIROUTER_PROVIDERS, UIView } from "ui- router-ng2 ";
11
- * import {MyConfig } from "./app/myConfig ";
10
+ * import {MyUIRouterConfig } from "./app/ router.config ";
11
+ * import {UIView } from "ui-router-ng2 ";
12
12
*
13
- * bootstrap(UIView, [
14
- * ...UIROUTER_PROVIDERS,
15
- * provide(UIRouterConfig, { useClass: MyConfig }
16
- * ]);
13
+ * @ UIRouterModule({
14
+ * providers: [provideUIRouter({ configClass: MyUIRouterConfig)]
15
+ * states: [state1, state2],
16
+ * bootstrap: [UIView]
17
+ * }) class RootAppModule {}
17
18
* ```
18
19
*
19
- * The application's initial states should be registered with the [[UIRouter.stateRegistry]].
20
20
* Any global configuration (transition hooks, parameter types, etc) should be done here.
21
21
*
22
22
* @example
23
23
* ```js
24
24
*
25
- * // myconfig.ts
26
- * import {STATES} from "./states";
25
+ * // router.config.ts
27
26
* import {registerAuthHook} from "./hooks";
28
27
* import {registerSlugType} from "./paramtypes";
29
28
*
30
- * export class MyConfig {
29
+ * export class MyUIRouterConfig {
31
30
* configure(uiRouter: UIRouter) {
32
- * STATES.forEach(state => uiRouter.stateRegistry.register(state));
33
31
* registerAuthHook(uiRouter.transitionService);
34
32
* registerSlugType(uiRouter.urlMatcherFactory);
35
33
* }
36
34
* }
37
35
*
38
- * // states.ts
39
- * import {FooComponent} from "./foo.component";
40
- * import {BarComponent} from "./bar.component";
41
- * import BAZ_MODULE_STATES from "./baz/states";
42
- *
43
- * export let STATES = [
44
- * { name: 'foo', url: '/url', component: FooComponent},
45
- * { name: 'bar', url: '/bar', component: BarComponent}
46
- * ].concat(BAZ_MODULE_STATES);
47
- *
48
36
* // hooks.ts
49
37
* export function registerAuthHook(transitionService: TransitionService) {
50
- * let requireAuthentication = (transition: Transition, injector: Injector) {
51
- * if (!Injector.get(AuthService).isAuthenticated()) {
52
- * return Injector.get(StateService).target('login');
38
+ * const requireAuthentication = (transition: Transition) => {
39
+ * let injector = transition.injector();
40
+ * if (!injector.get(AuthService).isAuthenticated()) {
41
+ * return injector.get(StateService).target('login');
53
42
* }
54
43
* }
44
+ *
55
45
* transitionService.onBefore({ to: (state) => state.requiresAuth }, requireAuthentication);
56
46
* }
57
47
*
@@ -64,6 +54,25 @@ import {UIRouter} from "../router";
64
54
* }
65
55
* ```
66
56
*
57
+ * Your configuration class can be injected, if necessary.
58
+ * Decorate with `@Injectable` and add dependencies to the class constructor.
59
+ *
60
+ * ```js
61
+ * @ Injectable()
62
+ * export class MyConfig {
63
+ * myService: MyService;
64
+ *
65
+ * constructor(myService: MyService) {
66
+ * this.myService = myService;
67
+ * }
68
+ *
69
+ * configure(router: UIRouter) {
70
+ * // ... use this.myService
71
+ * }
72
+ * }
73
+ * ```
74
+ *
75
+ *
67
76
*/
68
77
export class UIRouterConfig {
69
78
/**
0 commit comments