Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

routing navigate method not redirecting to targeted component

I have an issue with using routing navigate method of angular. I have two components : LoginComponent and HomeComponent. When I click on the button in "login.component.html", I want to be redirected to "home.component.html".

  • app.module.ts

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { AppComponent } from './app.component';
    import { HomeComponent } from './home/home.component';
    import { LoginComponent } from './login/login.component';
    import { RouterModule, Routes } from '@angular/router';
    
    const routes: Routes = [
      {path: 'home', component: HomeComponent}
    ];
    
    @NgModule({
      declarations: [
        AppComponent,
        HomeComponent,
        LoginComponent
      ],
      imports: [
        BrowserModule, RouterModule.forRoot(routes)
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    
  • login.component.html

 <button class="btn btn-primary" (click)="goHome();" class="clickable">Home</button>
  • home.component.html

<p>
  home works!
</p>

The URL changes but it remain in the same component page.

  • login.component.ts

    import { Component, OnInit } from '@angular/core';
    import { Router } from '@angular/router';
    
    @Component({
      selector: 'app-login',
      templateUrl: './login.component.html',
      styleUrls: ['./login.component.css']
    })
    export class LoginComponent implements OnInit {
    
      constructor(private router:Router) { }
    
      ngOnInit() {
      }
    
       goHome() {
        this.router.navigate(['home']);
      }
    }
    
like image 661
Youssef AIT ALI Avatar asked May 19 '18 17:05

Youssef AIT ALI


People also ask

What is redirect in vue router?

A redirect means when the user visits /home , the URL will be replaced by / , and then matched as / . But what is an alias? An alias of / as /home means when the user visits /home , the URL remains /home , but it will be matched as if the user is visiting / .

Can we add routerLink to button?

Can a button have routerLink Angular? After Angular v4 we can directly add a routerLink attribute on the anchor tag or button.

How to navigate in Ionic?

Navigation in Ionic works like a simple stack, where we push new pages onto the top of the stack, which takes us forwards in the app and shows a back button. To go backwards, we pop the top page off. Since we set this. navCtrl in the constructor, we can call this.


1 Answers

Make these additions:

<button class="btn btn-primary" (click)="goHome();" class="clickable">Home</button>

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

or via <a>.

<a [routerLink]="['/home']">home</a>

If necessary, remove / if you intend to be appended to the current route instead.

like image 95
Desu Avatar answered Oct 01 '22 06:10

Desu