问题描述
我创建了两个组件和一个服务,它们都在使用。我希望默认呈现一个组件,并从该组件内创建一个链接,该链接将在新选项卡上打开另一个组件,而无需更改 url(无路由)。
one.component.html
This is the default component and contains the link to open another component in a seperate tab but with same url.
<a (click)="openNewTab()"></a>
one.component.ts
// xyz code
window.open('/','_blank').focus()
two.component.html
<ng-template #two>
This should be opened in a seperate tab
</ng-template>
解决方法
您必须将第二个组件的完整 URL 路径传递到您的
window.open('/','_blank').focus()
方法。
- 目前您只是传递单个
/
,这意味着它不会重定向到您的第二个组件的特定路径。 - 尝试像这样更改 URL,
window.open('localhost:4000/secondURL','_blank').focus()
其中 secondURL
是您的第二个组件的 URL。