如何使用 NGRX 路由存储在组件之间导航并将我的 url 更改为localhost:4200/:id
:id
=> 表示:当前 ID
我希望我的 URL 更改为/id
我从组件接收到的好方法
我的导航栏
<nav class="background">
<button routerLink="product/1" routerLinkActive="active">Show</button> |
<button routerLink="product/add" routerLinkActive="active">Creat an Actions</button>
</nav>
但这种方法并不好。
我的应用模块
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 { }
在我的选择器里面
export const getSelectedPizza = createSelector(
getPizzasEntities,
getRouterState,
(entities, router): IPizza => {
return router.state && entities[router.state.params.pizzaId];
}
)
我想将它用于我的新组件。
如何从我的组件发送一个 id 并更改我的路由?
谢谢 :-)