0

I have a problem with angular router.

Currently I'm in my router /HOME and I'd like to route from a button from there to /HOME#id=123456

How can I do that? I'm trying something like:

<a class="app-card bg-petrol"[routerLink]="['/HOME']" [queryParams]="{'id':'123456'}">

But nothing happen - even nothing in the console.

edit: added routes file

app.routes.ts

{
  path: Routes.HOME,
  loadChildren: () => import('../routes/home/home.module').then(m => m.HomeModule),
  data: { hideNavSearch: true },
},

2 Answers 2

2

You should use queryparams with the link. For example :

<a [routerLink]="['HOME ']"  [queryParams]="{'id':'123456'}">
Sign up to request clarification or add additional context in comments.

Comments

0

In Angular, you need to define a parameterized path if there you want Angular to route to it. Add a new path in app.routing.module.ts with same name but having a parameter id.

app.routing.module.ts

@NgModule({
  declarations: [ ... ],
  imports: [
    RouterModule.forRoot([ 
      { path: 'HOME', component: HomeViewComponent }, 
      { path: 'HOME/:id', redirectTo: 'HomeViewComponent' } // add new route with id parameter
    ])
  ],
  exports: [
    RouterModule,
  ],
  providers: [],

})
export class AppRoutingModule {}

After that call your page like this:

<a routerLink="/HOME"  [queryParams]="{'id':'123456'}">

This will resolve your problem.

2 Comments

I've added my routing file, looks very different, how should I add the params?
Can you create a small stackblitz demo. It would be much more clear. Also, please add complete routing file with ony HOME route, as it will be enough to provide proper answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.