问题
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