Skip to content

Commit

Permalink
fix: clean up observables, updated docs
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts committed Jul 2, 2020
1 parent 0d2aa76 commit cba6db6
Show file tree
Hide file tree
Showing 5 changed files with 393 additions and 95 deletions.
295 changes: 212 additions & 83 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,98 +1,227 @@
# Reactiveangular
# angular-routing

This project was generated using [Nx](https://nx.dev).
A declarative router for Angular applications.

<p align="center"><img src="https://raw.githubusercontent.com/nrwl/nx/master/images/nx-logo.png" width="450"></p>
## Install

🔎 **Nx is a set of Extensible Dev Tools for Monorepos.**
Use your package manager of choice to install the package.

## Quick Start & Documentation
```sh
npm install angular-routing
```

[Nx Documentation](https://nx.dev/angular)
OR

[10-minute video showing all Nx features](https://nx.dev/angular/getting-started/what-is-nx)
```sh
yarn add angular-routing
```

[Interactive Tutorial](https://nx.dev/angular/tutorial/01-create-application)
## Usage

## Adding capabilities to your workspace
To register the Router, add the `RoutingModule.forRoot()` to your AppModule imports.

Nx supports many plugins which add capabilities for developing different types of applications and different tools.
```ts
import { RoutingModule } from 'angular-routing';

These capabilities include generating applications, libraries, etc as well as the devtools to test, and build projects as well.
@NgModule({
imports: [
// ... other imports
RoutingModule.forRoot()
]
})
export class AppModule {}
```

Below are our core plugins:
Or in a feature module

- [Angular](https://angular.io)
- `ng add @nrwl/angular`
- [React](https://reactjs.org)
- `ng add @nrwl/react`
- Web (no framework frontends)
- `ng add @nrwl/web`
- [Nest](https://nestjs.com)
- `ng add @nrwl/nest`
- [Express](https://expressjs.com)
- `ng add @nrwl/express`
- [Node](https://nodejs.org)
- `ng add @nrwl/node`
```ts
import { RoutingModule } from 'angular-routing';

There are also many [community plugins](https://nx.dev/nx-community) you could add.
@NgModule({
imports: [
// ... other imports
RoutingModule
]
})
export class FeatureModule {}
```

## Generate an application
After your components are registered, use the `Router` and `Route` components to register some routes.

Run `ng g @nrwl/angular:app my-app` to generate an application.

> You can use any of the plugins above to generate applications as well.
When using Nx, you can create multiple applications and libraries in the same workspace.

## Generate a library

Run `ng g @nrwl/angular:lib my-lib` to generate a library.

> You can also use any of the plugins above to generate libraries as well.
Libraries are sharable across libraries and applications. They can be imported from `@reactiveangular/mylib`.

## Development server

Run `ng serve my-app` for a dev server. Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.

## Code scaffolding

Run `ng g component my-component --project=my-app` to generate a new component.

## Build

Run `ng build my-app` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build.

## Running unit tests

Run `ng test my-app` to execute the unit tests via [Jest](https://jestjs.io).

Run `nx affected:test` to execute the unit tests affected by a change.

## Running end-to-end tests

Run `ng e2e my-app` to execute the end-to-end tests via [Cypress](https://www.cypress.io).

Run `nx affected:e2e` to execute the end-to-end tests affected by a change.

## Understand your workspace

Run `nx dep-graph` to see a diagram of the dependencies of your projects.

## Further help

Visit the [Nx Documentation](https://nx.dev/angular) to learn more.

## ☁ Nx Cloud

### Computation Memoization in the Cloud

<p align="center"><img src="https://raw.githubusercontent.com/nrwl/nx/master/images/nx-cloud-card.png"></p>

Nx Cloud pairs with Nx in order to enable you to build and test code more rapidly, by up to 10 times. Even teams that are new to Nx can connect to Nx Cloud and start saving time instantly.

Teams using Nx gain the advantage of building full-stack applications with their preferred framework alongside Nx’s advanced code generation and project dependency graph, plus a unified experience for both frontend and backend developers.

Visit [Nx Cloud](https://nx.app/) to learn more.
```html
<router>
<route path="/blog/**">
<app-blog *routeComponent></app-blog>
</route>
<route path="/posts/:postId">
<app-post *routeComponent></app-post>
</route>
<route path="/about">
<app-about *routeComponent></app-about>
</route>
<route path="/" redirectTo="/blog">
</route>
<route path="**">
<app-page-not-found *routeComponent></app-page-not-found>
</route>
</router>
```

## Navigating with Links

Use the `linkTo` directive with a _full path_ to register links handled by the router.

```html
<a linkTo="/">Home</a>
<a linkTo="/about">About</a>
<a linkTo="/blog">Blog</a>
```

## Adding classes to active links

To add classes to links that match the current URL path, use the `linkActive` directive.

```html
<a linkTo="/" linkActive="active">Home</a>
<a linkTo="/about" linkActive="active">About</a>
<a linkTo="/blog" linkActive="active">Blog</a>
```

## Using the Router service

To navigate from a component class, or get global route information, such as the current URL, or hash fragment, inject the `Router` service.

```ts
import { Component } from '@angular/core';
import { Router } from 'angular-routing';

@Component({...})
export class MyComponent {
constructor(private router: Router) {}

ngOnInit() {
this.router.url$.subscribe();
this.router.hash$.subscribe();
}

goHome() {
this.router.go('/');
}
}
```

## Using Route Params

To get the route params, inject the `RouteParams` observable. Provide a type for the shape of the route params object.

```ts
import { Component } from '@angular/core';
import { RouteParams } from 'angular-routing';

@Component({...})
export class MyComponent {
constructor(
private routeParams$: RouteParams<{ postId: string }>
) {}

ngOnInit() {
this.routeParams$.subscribe(console.log);
}
}
```

## Using Query Params

To get the route params, inject the `QueryParams` observable. Provide a type for the shape of the query params object.

```ts
import { Component } from '@angular/core';
import { QueryParams } from 'angular-routing';

@Component({...})
export class MyComponent {
constructor(
private queryParams$: QueryParams<{ debug: boolean }>
) {}

ngOnInit() {
this.queryParams$.subscribe(console.log);
}
}
```

## Lazy Loading Modules

To lazy load a module, use the `load` binding on the `route` component.

```ts
import { Component } from '@angular/core';

@Component({
template: `
<router>
<route path="/lazy/**" [load]="modules.lazy">
</route>
</router>
`
})
export class MyComponent {
modules = {
lazy: () => import('./lazy/lazy.module').then(m => m.LazyModule)
}
}
```

Register a component to register the child routes.

```ts
import { NgModule, Component } from '@angular/core';
import { ModuleWithRoute } from 'angular-routing';

@Component({
template: `
<router>
<route path="/">
<app-lazy *routeComponent></app-lazy>
</route>
</router>
`
})
export class LazyRouteComponent { }
```

Implement the `ModuleWithRoute` interface for the route component to render after the module is loaded.

```ts
@NgModule({
declarations: [
LazyRouteComponent,
LazyComponent
]
})
export class LazyModule implements ModuleWithRoute {
routeComponent = LazyRouteComponent;
}
```

## Lazy Loading Components

To lazy load a component, use the `load` binding on the `route` component.

```ts
import { Component } from '@angular/core';

@Component({
template: `
<router>
<route path="/lazy" [load]="components.lazy">
</route>
</router>
`
})
export class MyComponent {
components = {
lazy: () => import('./lazy/lazy.component').then(m => m.LazyComponent)
}
}
```

0 comments on commit cba6db6

Please sign in to comment.