All Projects → angular-component → Router

angular-component / Router

Licence: mit
Angular Component Router - A declarative router for Angular applications

Programming Languages

typescript
32286 projects
declarative
70 projects

Projects that are alternatives of or similar to Router

Router
🍭灵活的组件化路由框架.
Stars: ✭ 1,502 (+567.56%)
Mutual labels:  router, component
Routing
The Routing component maps an HTTP request to a set of configuration variables.
Stars: ✭ 7,080 (+3046.67%)
Mutual labels:  router, component
Component
🔥🔥🔥A powerful componentized framework.一个强大、100% 兼容、支持 AndroidX、支持 Kotlin并且灵活的组件化框架
Stars: ✭ 2,434 (+981.78%)
Mutual labels:  router, component
Apprun
AppRun is a JavaScript library for developing high-performance and reliable web applications using the elm inspired architecture, events and components.
Stars: ✭ 1,087 (+383.11%)
Mutual labels:  router, component
Brouter
Stars: ✭ 198 (-12%)
Mutual labels:  router, component
React Native Typing Animation
💬 A typing animation for your React Native chat app based on simple trigonometry to create better loaders.
Stars: ✭ 211 (-6.22%)
Mutual labels:  component
Vue Tinymce Editor
This a component provides use of tinymce for vue developers
Stars: ✭ 216 (-4%)
Mutual labels:  component
React Native Action Button
customizable multi-action-button component for react-native
Stars: ✭ 2,408 (+970.22%)
Mutual labels:  component
React Reorder
Drag & drop, touch enabled, reorderable / sortable list, React component
Stars: ✭ 209 (-7.11%)
Mutual labels:  component
React Messenger Customer Chat
React component for messenger customer chat plugin
Stars: ✭ 221 (-1.78%)
Mutual labels:  component
Router
Android 平台一个简单的路由框架,包含路由和参数注入两部分功能。
Stars: ✭ 219 (-2.67%)
Mutual labels:  router
Yoshino
A themable React component library!Flexible Lightweight PC UI Components built on React! Anyone can generate easily all kinds of themes by it!
Stars: ✭ 216 (-4%)
Mutual labels:  component
Vuejs Tree
A highly customizable and blazing fast Vue tree component ⚡🌲
Stars: ✭ 211 (-6.22%)
Mutual labels:  component
V Bar
The virtual responsive crossbrowser scrollbar component for VueJS 2x
Stars: ✭ 216 (-4%)
Mutual labels:  component
Browser Kit
The BrowserKit component simulates the behavior of a web browser, allowing you to make requests, click on links and submit forms programmatically.
Stars: ✭ 2,563 (+1039.11%)
Mutual labels:  component
Sheet Router
fast, modular client-side router
Stars: ✭ 219 (-2.67%)
Mutual labels:  router
React Instagram Embed
React embedding Instagram posts component
Stars: ✭ 209 (-7.11%)
Mutual labels:  component
Skipper
An HTTP router and reverse proxy for service composition, including use cases like Kubernetes Ingress
Stars: ✭ 2,606 (+1058.22%)
Mutual labels:  router
Mvvmframe
🏰 MVVMFrame for Android 是一个基于Google官方推出的Architecture Components dependencies(现在叫JetPack){ Lifecycle,LiveData,ViewModel,Room } 构建的快速开发框架。有了MVVMFrame的加持,从此构建一个MVVM模式的项目变得快捷简单。
Stars: ✭ 218 (-3.11%)
Mutual labels:  component
Axs
Stupid simple style components for React
Stars: ✭ 214 (-4.89%)
Mutual labels:  component

Angular Component Router

npm version

All Contributors

A declarative router for Angular applications.

Install

Use your package manager of choice to install the package.

npm install @angular-component/router

OR

yarn add @angular-component/router

Installation with ng add

You can use ng add to install the package by using the command below.

ng add @angular-component/router

The above command will install the package, and add the ComponentRouterModule import in the AppModule.

Usage

To register the Router, add the ComponentRouterModule.forRoot() to your AppModule imports.

import { ComponentRouterModule } from '@angular-component/router';

@NgModule({
  imports: [
    // ... other imports
    ComponentRouterModule.forRoot(),
  ],
})
export class AppModule {}

Or in a feature module

import { ComponentRouterModule } from '@angular-component/router';

@NgModule({
  imports: [
    // ... other imports
    ComponentRouterModule,
  ],
})
export class FeatureModule {}

After your components are registered, use the Router and Route components to register some routes.

<router>
  <!-- For nested routes use exact: false -->
  <route path="/blog" [exact]="false">
    <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="/" [exact]="false">
    <app-page-not-found *routeComponent></app-page-not-found>
  </route>
</router>

Route sorting

Angular routing is sorting the routes upon registration, based on priority. The order in which the routes are defined in your template is therefore not important.

The following two examples will give the same results

<router>
  <route path="/blog" [exact]="false">
    <app-blog *routeComponent></app-blog>
  </route>
  <route path="/" redirectTo="/blog"></route>
  <route path="/" [exact]="false">
    <app-page-not-found *routeComponent></app-page-not-found>
  </route>
</router>

and

<router>
  <route path="/" [exact]="false">
    <app-page-not-found *routeComponent></app-page-not-found>
  </route>
  <route path="/" redirectTo="/blog"></route>
  <route path="/blog" [exact]="false">
    <app-blog *routeComponent></app-blog>
  </route>
</router>

The sorting algorithm has only a few rules (ordered by importance):

  • Named routes (e.g. /blog) have priority over root route (/)
  • Static routes (e.g. /blog/view) have priority over parametrized (e.g. /blog/:id)
  • Exact route (with exact set to true or omitted) has priority over non-exact (with exact set to false)
  • Longer paths have priority over shorter

Route restrictions

Implementing the route restriction is as simple as adding a structural directive on a route component

<router>
  <route path="/admin" *ngIf="user.isAuthenticated$ | async">
    <app-admin *routeComponent></app-admin>
  </route>
  <route path="/admin" *ngIf="!(user.isAuthenticated$ | async)">
    <app-login *routeComponent></app-login>
  </route>
</router>

The restriction doesn't stop the navigation. It simply removes the route from the configuration so the next eligible route will pick it up.

Navigating with Links

Use the linkTo directive with a full or relative path to register links handled by the router.

<a linkTo="/">Home</a>
<a linkTo="/about">About</a>
<a linkTo="/blog">Blog</a>
<a linkTo="../other"></a>

Adding classes to active links

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

<a linkTo="/" linkActive="active">Home</a>
<a linkTo="/about" linkActive="active">About</a>
<a linkTo="/blog" linkActive="active" [activeOptions]="{ exact: false }"
  >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.

import { Component } from '@angular/core';
import { Router } from '@angular-component/router';

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

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

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

  goForward() {
    this.router.forward();
  }

  goBack() {
    this.router.back();
  }
}

Using Route Params

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

import { Component } from '@angular/core';
import { RouteParams } from '@angular-component/router';

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

import { Component } from '@angular/core';
import { QueryParams } from '@angular-component/router';

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

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

@Component({
  template: `
    <router>
      <route path="/lazy" [exact]="false" [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.

import { NgModule, Component } from '@angular/core';
import { ModuleWithRoute } from '@angular-component/router';

@Component({
  template: `
    <router>
      <route path="/">
        <app-lazy *routeComponent></app-lazy>
      </route>
      <route path="/" [exact]="false" redirectTo="/404"> </route>
    </router>
  `,
})
export class LazyRouteComponent {}

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

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

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

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Brandon

💻 📖 ⚠️

Miroslav Jonaš

💻 📖 ⚠️

Santosh Yadav

💻 📖

Kyle Cannon

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].