2

In the project I work with Angular, I want it to go to the other component when the button is clicked, but it does not. I connected the navigate method to the button in the menu and wrote the method but it didn't. How can I fix?

.html

<button mat-menu-item (click)="navigate()">Reservations List</button>

.ts

navigate() {
    this.router.navigate(['reservationdetail']);
}

.routing

  { path: 'reservationdetail', component: ReservationdetailComponent }

.app module

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HotelComponent } from './hotel/hotel.component';
import { ReservationdetailComponent } from ' 
./reservationdetail/reservationdetail.component'; 
import { RezervasyonComponent } from './rezervasyon/rezervasyon.component';

export const routes: Routes = [
{ path: 'hotel', component: HotelComponent },
{ path: "**", redirectTo: "hotel", pathMatch: "full" },
{ path: 'rezervazyon', component: RezervasyonComponent },
{ path: 'reservationdetail', component: ReservationdetailComponent }
];

@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
3
  • You need router-outlet to render the dynamically routed component angular.io/api/router/RouterOutlet Commented Apr 14, 2020 at 13:36
  • Check your browser console for any errors - this may be helpful. Or post more of your code here Commented Apr 14, 2020 at 13:39
  • I'm not getting an error. I edited the code in more detail. I applied it to the bottom, and I got the Can't bind to 'routerLink' since it isn't a known property of 'button' error. Commented Apr 14, 2020 at 14:10

2 Answers 2

3

If only navigate in function u don't to navigate in function . U can navigate from html

<button mat-menu-item [routerLink]="['/reservationdetail']">Reservations List</button>
Sign up to request clarification or add additional context in comments.

12 Comments

hocam yardımın için teşekkürler ama şöyle bir hata aldım onu çözmeye çalışıyorum şimdi. Can't bind to 'routerLink' since it isn't a known property of 'button'. ("u"> <button mat-menu-item (click)="search2()">Price Search</button> <button mat-menu-item [ERROR ->][routerLink]="['/reservationdetail']">Reservations List</button> </mat-menu>
App.modules içine routermodülü import ettin mi? import { RouterModule } from '@angular/router';
evet onu çok önceden yaptım zaten başka route işlemleri için hatta şimdi de baktım yapmışım zaten.
App.modules de soru içinde paylaşır mısın?
Posts and comments should be in English.
|
1

Do you have this route setup in app.module.ts at root level?

if not, you must be having other modules route before your component.

you make sure you write it like,

this.router.navigate(['/root/parent/../reservationdetail']);

that will work 😊

UPDATE

export const routes: Routes = [
{ path: 'hotel', component: HotelComponent },
{ path: 'rezervazyon', component: RezervasyonComponent },
{ path: 'reservationdetail', component: ReservationdetailComponent },
{ path: "**", redirectTo: "hotel" }
];

can you update like this?

13 Comments

I edited the code and shared it. I installed the app module up. This method does not work unfortunately
can you tell me hierarchical structure of your app from root to this component. I am asking this because it is usually easy to achieve.
I am writing a hotel project. There is a button when my page first opened. Clicking the button, two options appear. The first option is the section where the search screen works properly, but the second option should go to the reservation detail page, but I could not do it.
I guess that's the catch, you must be having some sort of path for first page, can you check that. is 1st page opening at localhost:4200
hotel component opens on the first page. Because I wrote it as root, you can look at the code above.
|

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.