Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - Add target to router navigate

What I want to do is to open the target of router navigate in another tab or in a popup. My instruction is this:

private router: Router;
this.router.navigate(['/page', id]);

In routing I have:

 const routes: Routes = [
{
    path: '',
    component: LayoutComponent,
    children: [
        { path: 'page', loadChildren: './page/page.module#PageModule' }
    ]
}
];

I would like to open this link in another tab or in popup window. What can I do?


this is the code of page.ts

@Component({
 selector: 'app-etichetta',
 templateUrl: './page.component.html',
 styleUrls: ['./page.component.scss'],
 animations: [routerTransition()]
})
export class PageComponent implements OnInit {


 constructor(private router: Router,
    private route: ActivatedRoute,
    public appService: AppService) {
 }


 ngOnInit() {

 }
}

and this is the html:

<div [@routerTransition]>
  <br>
  <div class="row">

  </div>
</div>
like image 346
Martina Avatar asked Dec 02 '18 12:12

Martina


2 Answers

import { Location } from '@angular/common';
import { Router } from '@angular/router';

export class YourComponent {

   constructor(private router: Router){}

   openNewTab (){
           const host: string =  location.origin;
           const url: string = host + '/#/' + String(this.router.createUrlTree(['/main/product'], { queryParams: { key: encryptData } }));
           window.open(url, '_blank');

   }
}
like image 146
Lional J Avatar answered Sep 17 '22 22:09

Lional J


To redirect manually you should first create an URL where to redirect using createUrlTree method, and then redirect.

const url = this.router.createUrlTree(['/page', id])
window.open(url.toString(), '_blank')

Declarative navigation should work.

<a target="_blank" [routerLink]="['/page', id]">
  Link
</a>
like image 38
Pankaj Parkar Avatar answered Sep 19 '22 22:09

Pankaj Parkar