0.0.32 • Published 8 years ago

angular2-route-service v0.0.32

Weekly downloads
4
License
MIT
Repository
github
Last release
8 years ago

angular2-route-service

An implementation of router wrapper at Angular2 (2.0.0 compatible).

Dependencies

Installation

First you need to install the npm module:

npm install angular2-route-service --save

Features

  1. Support a Flux application architecture (state, dispatcher).
  2. Allows transform queryParams to matrix params when dom content is loaded ($$PREVENT_QUERY_TRANSFORM = true allows disable the feature).

Use

main.ts

import {RouteServiceModuleFactory} from "angular2-route-service/index";

@NgModule({
    bootstrap: [ApplicationComponent],
    imports: [
        ...
        // If your application does not have its own the state and the dispatcher
        // See RouteState and RouteDispatcher (from angular2-route-service)
        RouteServiceModuleFactory.makeModule(),
        
        // Or you have the application state and the dispatcher
        // RouteServiceModuleFactory.makeModule(State, Dispatcher)
        
        RouterModule.forRoot([
           ...
        ])
    ],
    ...
})
export class ApplicationModule {
    constructor() {
    }
}

app.ts

import {RouteService} from "angular2-route-service/index";

@Component(...)
export class AppComponent {
    constructor(@Inject(RouteService) routeService: RouteService, // You have to inject route service at least once!
                @Inject(Store) store:Store) {
    }
}

routeConfig.ts

export const AboutRoutes: Route[] = [
  {
    path: 'about',
    component: AboutComponent,
    data: {configValue: 'aboutPageConfigValue'}
  }
];

store.ts

import {
	IRouteEventPayload,
	RouteDispatcher,
	RouteState, RouteService
} from 'angular2-route-service/index';

...

@Injectable()
export class Store {

	constructor(@Inject(RouteDispatcher) private dispatcher: RouteDispatcher, // When "RouteServiceModuleFactory.makeModule()" is used.
	            @Inject(RouteState) private state: RouteState,
	            @Inject(RouteService) private routeService: RouteService) {

        dispatcher.navigationEnd.subscribe((payload: IRouteEventPayload) => {
            console.log('Route name is:', this.state.routeSnapshot.name);
            console.log('Route params are:', this.state.routeSnapshot.params);
            console.log('Route config is:', this.state.routeSnapshot.config);
            console.log('Path is:', payload.path);
            
            console.log('Page is ready: ', this.state.isPageReady());

            if (this.state.routeSnapshot.name === 'about') {
                setTimeout(() => {
                    this.routeService.go(''/*, {indexQuery: 100500} */);
                }, 1000);
            }
        });
	}
}

Preview

page.ts

@Component(...)
export class Page  {

    constructor(@Inject(RouteState) private state: RouteState) {
    }

    get pageReady(): boolean {
        return this.state.isPageReady();
    }
}

page.html

<div *ngIf="pageReady">
  Page is ready!<br>
  You can render here your components, depending on the route params
</div>
<div *ngIf="!pageReady" class="loading"></div>

Publish

npm run deploy

License

Licensed under MIT.

0.0.32

8 years ago

0.0.31

8 years ago

0.0.30

8 years ago

0.0.29

8 years ago

0.0.28

8 years ago

0.0.27

8 years ago

0.0.25

8 years ago

0.0.24

8 years ago

0.0.23

8 years ago

0.0.22

8 years ago

0.0.21

8 years ago

0.0.20

8 years ago

0.0.19

8 years ago

0.0.17

8 years ago

0.0.16

8 years ago

0.0.15

8 years ago

0.0.14

8 years ago

0.0.13

8 years ago

0.0.12

8 years ago

0.0.11

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago