简体   繁体   English

如何使用来自angular 2组件的路由器打开新的浏览器选项卡?

[英]How to open new browser tab using router from angular 2 component?

I have a angular 2 component and need to navigate to another route but in different browser tab.我有一个 angular 2 组件,需要导航到另一条路线,但在不同的浏览器选项卡中。 Below code is navigating in same browser tab.下面的代码在同一个浏览器选项卡中导航。

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

    @Component({
         templateUrl: 'quotes-edit.component.html'
    })

    export class QuoteComponent {
         constructor(private router: Router) {
         }

         this.router.navigate(['quote/add']);
    }

Angular applications are SPA(Single Page Application), which means that the Router is nothing else than a Module to mimic the change of a Route which wouldn't be necessary. Angular 应用程序是 SPA(单页应用程序),这意味着路由器只不过是一个模块,用于模拟不必要的路由更改。 You can open redirect in another route in another tab like this您可以像这样在另一个选项卡中的另一个路由中打开重定向

  /**
   * Open url in a new tab
   */
  navigateToNewTab(): void {
    // Here you can get the url
    const { protocol, host } = window.location;
    // Or use the router
    // this.router.url

    const path = '...';

    const url = `${protocol}//${host}/${path}`;
    window.open(url,'_blank');
  }


You wouldn't use the angular router for this but instead, you could make a function that has the following您不会为此使用角度路由器,而是可以创建一个具有以下功能的函数

 openinbrowserclicked(url) {
    window.open(
      'url',
      '_blank',
      'toolbar=no,resizable=yes,scrollbars=yes'
    );
  }

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

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