How to navigate in angular2 to new tab

可紊 提交于 2020-01-02 07:57:43

问题


Is there a parameter to provide to the router so that router.navigate() will open in a new tab/window in browser?


回答1:


Create routes just like instructed here.

Set up your app-routing.module.ts ( or just put it in app.module.ts ), place the <router-link> in your app.component.html.

Now, to open a new window from the main page, ( or whatever sub-routes you have created ) just call a function, handled in the main component, that will point to the url of the sub-component route that you setup for the new window.

Example snippits:

app-routing.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { MainComponent } from './main/main.component'; // main window
import { JclInfoComponent } from './jcl-info/jcl-info.component'; // new window/tab

const appRoutes: Routes = [
    { path: '', component: MainComponent }, // main route
    { path: 'openJcl', component: JclInfoComponent } // new window route
];

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

app.component.html

<router-outlet>
    <!-- Just showing you that you can put things inside this. -->
    <template ngbModalContainer></template> 
</router-outlet>

main.component.html

<button (click)="testMe()">myLabel</button>

main.component.ts

public testMe() {
    window.open( "openJcl" );
}


来源:https://stackoverflow.com/questions/41096375/how-to-navigate-in-angular2-to-new-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!