简体   繁体   English

Angular - 将目标添加到路由器导航

[英]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这是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:这是 html:

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

  </div>
</div>

To redirect manually you should first create an URL where to redirect using createUrlTree method, and then redirect.要手动重定向,您应该首先使用createUrlTree方法创建一个重定向的 URL,然后重定向。

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

Declarative navigation should work.声明式导航应该有效。

<a target="_blank" [routerLink]="['/page', id]">
  Link
</a>

Angular 不支持导航到新选项卡,您可以使用 window.open 来执行此操作

window.open(url, '_blank');

在角度组件 html 文件中使用它

<a class="btn btn-primary" target="_blank" routerLink="/create-playlist" > Create Playlist </a>

The router doesn't have an option to open the link in a new tab.路由器没有在新选项卡中打开链接的选项。 That said, there is a workaround – you can use target="_blank" of a hidden link in your template, reference it and click it:也就是说,有一个解决方法 - 您可以在模板中使用隐藏链接的target="_blank" ,引用它并单击它:

<a #link
  style="visibility: hidden; display: none;" 
  [routerLink]="['/page', id]"
  target="_blank"
  queryParamsHandling="merge"
></a>

@ViewChild('link', { static: false }) private link: ElementRef;

this.link.nativeElement.click();
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');

   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM