main.component.htmlhtml
<a routerLink="/about"> About </a>
複製代碼
在上面到例子中,想解決以下問題:瀏覽器
如何路由導航到新選項卡(新窗口)?bash
或者說如何使router.navigate()將在瀏覽器的新選項卡/窗口中打開?angular2
假設路由已配置完成,以下例子:app
app-routing.module.tsui
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { MainComponent } from './main/main.component'; // main window
import { AboutComponent } from './about/about.component'; // new window/tab
const appRoutes: Routes = [
{ path: '', component: MainComponent }, // main route
{ path: 'about', component: AboutComponent } // new window route
];
@NgModule({
imports: [
RouterModule.forRoot( appRoutes )
],
exports: [
RouterModule
]
})
export class AppRoutingModule {}
複製代碼
app.component.htmlspa
<router-outlet></router-outlet>
複製代碼
使用<a>
標籤的 target 屬性code
main.component.htmlcomponent
<a target="_blank" routerLink="/about"> About </a>
複製代碼
使用window.open()方法router
main.component.html
<button (click)="gotoAbout()"> About </button>
複製代碼
main.component.ts
gotoAbout(){
window.open( "about" );
}
複製代碼