How to navigate between components with NGRX route store and change my url to localhost:4200/:id

1.5k Views Asked by At

How to navigate between components with NGRX route store and change my url to localhost:4200/:id

:id => means: current id

I want my URL change with /id that I receive from component in good way

My navbar

<nav class="background">
    <button routerLink="product/1" routerLinkActive="active">Show</button> |
    <button routerLink="product/add" routerLinkActive="active">Creat an Actions</button>
</nav>

But this approach isn't good.

My app module

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { RouterModule, Routes } from '@angular/router';

import { StoreModule } from '@ngrx/store';
import { EffectsModule } from '@ngrx/effects';
import { RouterStateSerializer, StoreRouterConnectingModule, RouterState } from '@ngrx/router-store';
import { reducer, CustomSerializer } from './storeRouter';
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
import { reducers, effects } from './store';

import { AppRoutingModule } from './app-routing.module';
import { environment } from '../environments/environment';
import { AppComponent } from './app.component';
import { NavbarComponent } from './navbar/navbar.component';
import { AddComponent } from './add/add.component';
import { ReadComponent } from './read/read.component';

const root: Routes = [
  {
    path: 'product', children: [
      { path: 'add', component: AddComponent },
      { path: ':pizzaId', component: ReadComponent },
    ]
  },
  { path: '**', redirectTo: 'read', pathMatch: 'full' }
]

@NgModule({
  declarations: [
    AppComponent,
    AddComponent,
    ReadComponent,
    NavbarComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule,
    HttpClientModule,

    StoreRouterConnectingModule.forRoot({
      routerState: RouterState.Minimal,
    }),

    EffectsModule.forRoot(effects),
    RouterModule.forRoot(root),

    StoreModule.forRoot(reducer, {
      runtimeChecks: {
        strictStateImmutability: true,
        strictActionImmutability: true,
        strictStateSerializability: true,
        strictActionSerializability: true,
      },
    }),

    StoreModule.forFeature('products', reducers),

    // Only a tool for developers will delete on products
    StoreDevtoolsModule.instrument({
      maxAge: 25,
      logOnly: environment.production,
    }),

  ],
  providers: [{ provide: RouterStateSerializer, useClass: CustomSerializer }],
  bootstrap: [AppComponent]
})
export class AppModule { }

inside my selectors

export const getSelectedPizza = createSelector(
  getPizzasEntities,
  getRouterState,
  (entities, router): IPizza => {
    return router.state && entities[router.state.params.pizzaId];
  }
)

I want to use it for my new component.

How can I send an id from my component and change my routes?

Thank you :-)

1

There are 1 best solutions below

3
beanic On

Follow steps of this article and you will get what you want https://angular-2-training-book.rangle.io/routing/routeparams