33

I'm Trying to redirect one component to another (like page redirect)

Angular link

here is my code

app-routing.module.ts

import { RoleComponent } from './role/role.component';
import { AppComponent } from './app.component';
import { NgModule }             from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [
  { path: '', redirectTo: '/AppComponent', pathMatch: 'full' },
  { path: 'role', component: RoleComponent },

];

@NgModule({
  imports: [ RouterModule.forRoot(routes) ],
  exports: [ RouterModule ]
})
export class AppRoutingModule {}

top-header.component.html

 <ul class="navbar-nav mr-auto">
       <li class="nav-item">
          <a class="nav-link" href="#">Verified</a>
        </li>
        <li class="nav-item">
          <a routerLink="/role" class="nav-link" href="#">ROLES</a>
        </li>
      </ul>

AppComponent.html

<div class="container p-md-0">
<app-top-header></app-top-header>
<router-outlet></router-outlet>
<app-main-contain></app-main-contain>
</div>
12
  • Please configure routers properly.You haven't mention router for /AppComponent. Commented May 15, 2018 at 5:51
  • You told about this code ? {path: 'AppComponent' , component: AppComponent}, . When i put this code design display multiple time . Commented May 15, 2018 at 6:05
  • Instead of forRoot(routes) you just use RouterModule.forChild(ROUTES) for lazy loading and please attach app.module.ts file here. Commented May 15, 2018 at 6:17
  • @NgModule({ declarations: [ AppComponent, TopHeaderComponent, AlertComponent, MainContainComponent, FooterComponent, RoleComponent, ], imports: [ BrowserModule, AppRoutingModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { } Commented May 15, 2018 at 6:20
  • 1
    Remove { path: '', redirectTo: '/AppComponent', pathMatch: 'full' }, line and check. Commented May 15, 2018 at 6:22

6 Answers 6

32

You can use router.navigate function of the Router class. Just import Router from @angular/router and create a object for it in the constructor(private router: Router) and use router.navigate(['/roles']);

import { RoleComponent } from './role/role.component';
import { AppComponent } from './app.component';
import { NgModule }             from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [
  { path: '', redirectTo: '/AppComponent', pathMatch: 'full' },
  { path: 'role', component: RoleComponent },

];

@NgModule({
  imports: [ RouterModule.forRoot(routes) ],
  exports: [ RouterModule ]
})
export class AppRoutingModule {
  constructor(private router: Router)
}

functionOnWhichRedirectShouldHappen(){
  this.router.navigate(['/role']);
}

HTML

<ul class="navbar-nav mr-auto">
   <li class="nav-item">
       <a class="nav-link" href="#">Verified</a>
   </li>
   <li class="nav-item">
      <button (click)="functionOnWhichRedirectShouldHappen()">ROLES</button>
   </li>
</ul>
Sign up to request clarification or add additional context in comments.

3 Comments

in wiche page ?
@DhrupalSuthar Check the answer now
And you can use routerLink too. Use it like [routerLink]="['/role']"
23

Your app-routing.module.ts is fine. Now, in your component you can do Import Router and use it to redirect.

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {

  constructor(private router: Router) { }

  ngOnInit() { }

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

}

And in your component.html just call that redirect() function

<button (click)="redirect()"></button>

Comments

1

just add this path in your app-routing.module.ts file so that it can know the path:

{ path: 'AppComponent', component: AppComponent }

1 Comment

Welcome to Stackoverflow. Suggest you to edit the code with code formatting. For more information on code formatting, refer this meta post meta.stackoverflow.com/questions/251361/…
1

I used this solution :

  redirectTo() {
    window.history.back()
  }

Comments

0

You can simply redirect your page to another one by adding this to your app-routing.module.ts

file :

{ path: '', redirectTo: 'component route name', pathMatch: 'full' },
{ path: 'component route name', component: MyComponent },

Comments

-3

Try below code

You can use Angular $window

$window.location.href = '/index.html';

Example usage in a contoller:

(function () {
    'use strict';

    angular
        .module('app')
        .controller('LoginCtrl', LoginCtrl);

    LoginCtrl.$inject = ['$window', 'loginSrv', 'notify'];

    function LoginCtrl($window, loginSrv, notify) {
        /* jshint validthis:true */
        var vm = this;
        vm.validateUser = function () {
             loginSrv.validateLogin(vm.username, vm.password).then(function (data) {          
                if (data.isValidUser) {    
                    $window.location.href = '/index.html';
                }
                else
                    alert('Login incorrect');
            });
        }
    }
})();

1 Comment

The question is related to Angular, not AngularJS which is what your answer uses. The two work quite differently.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.