简体   繁体   English

如何在新选项卡中打开 Angular 组件?

[英]How to open a Angular component in a new tab?

I have a considerable amount of data to show on the screen.我有大量数据要显示在屏幕上。 I need to present a simplified list so the user can choose one of the items and see it's details.我需要提供一个简化的列表,以便用户可以选择其中一项并查看其详细信息。

So imagine I have a component SimpleListComponent, it will hold the data and present a simplified view所以想象一下我有一个组件 SimpleListComponent,它将保存数据并呈现一个简化的视图

export class SimpleListComponent{
    @Input() data: any[];
}

The html HTML

<ul>
    <li *ngFor="let item of data">
        <a>{{item.name}}</a>
    </li>
</ul>

The user should be able to click on one of the itens and open in a new tab a view with that item's details.用户应该能够单击其中一个项目并在新选项卡中打开包含该项目详细信息的视图。 So if I have a second Component所以如果我有第二个组件

export class DetailedViewComponent{
    @Input() item: any;
}
<div>
    <!--All fields of item here-->
</div>

Edit: The catch here is that I'm presenting data from a very customized search, so I don't have a ID to get the details from the server or any other way to get the data again.编辑:这里要注意的是,我正在展示来自非常定制的搜索的数据,所以我没有 ID 来从服务器获取详细信息,也没有任何其他方式来再次获取数据。 So the only way is to, somehow, pass the data that is already loaded.所以唯一的方法是以某种方式传递已经加载的数据。

How can I achieve that in angular?我怎样才能以角度实现这一目标? Give the item data to the second component and open it in a new tab?把item数据给第二个组件,然后在新标签页打开?

I know this is an old post but since this is the first result when you search “how to open angular component in new tab/window” I wanted to post an answer.我知道这是一篇旧帖子,但由于这是您搜索“如何在新选项卡/窗口中打开角度组件”时的第一个结果,所以我想发布一个答案。

If you create a route for the component and load the route in a new tab you will end up bootstrapping the app again.如果您为组件创建路由并在新选项卡中加载该路由,您最终将再次引导应用程序。

So if you want to open an angular component without bootstrapping the whole app you can do it using angular portals.因此,如果您想在不引导整个应用程序的情况下打开一个角度组件,您可以使用角度门户来完成。 You can also easily pass data between the parent and the child window.您还可以轻松地在父窗口和子窗口之间传递数据。

I recently has this requirement and I couldn't find any comprehensive resource on this so I put together an article on it.我最近有这个需求,但我找不到任何全面的资源,所以我整理了一篇关于它的文章。

https://medium.com/@saranya.thangaraj/open-angular-component-in-a-new-tab-without-bootstrapping-the-whole-app-again-e329af460e92?sk=21f3dd2f025657985b88d6c5134badfc https://medium.com/@saranya.thangaraj/open-angular-component-in-a-new-tab-without-bootstrapping-the-whole-app-again-e329af460e92?sk=21f3dd2f025657985b88d6c5134badfc

Here is a live demo of the example app这是示例应用程序的现场演示

https://stackblitz.com/edit/portal-simple https://stackblitz.com/edit/portal-simple

You can create a routing for DetailedViewComponent and ""您可以为 DetailedViewComponent 和“”创建路由

In your routing:在您的路由中:

{
    path: 'detailed/:id',
    component: DetailedViewComponent
}

And after that On typeScript of SimpleListComponent:然后在 SimpleListComponent 的 typeScript 上:

public detailedPath;
ngOnInit() {
     this.detailedPath = window.location.origin + '/detailed/';
}

On your Html of SimpleListComponent:在 SimpleListComponent 的 Html 上:

<ul>
   <li *ngFor="let item of data">
      <a href="{{detailedPath + item.id}}" target="_blank">
   </li>
</ul>

On TypeStript of DetailedViewComponent:在 DetailedViewComponent 的 TypeStript 上:

public id;
constructor(private routeParams: ActivatedRoute) {
}

ngOnInit() {
    this.routeParams.params.subscribe(params => {
      this.id = parseInt(params['id']);
    });
    //Some logic to get the details of this id
}

In case someone runs into the same issue I ran:如果有人遇到我遇到的同样问题:

I ended using localstorage to temporarily store my object and access it from the other window.我结束了使用localstorage临时存储我的对象并从另一个窗口访问它。

So the code ended up like:所以代码最终变成了这样:

<a target="_blank" [routerLink]="['/details', item.name]" click="passObject(item.name)">
passObject(i){
    localStorage.setItem('i.name', JSON.stringify(i));
}

And in the details component:在详细信息组件中:

ngOnInit() {
    this.item = JSON.parse(localStorage.getItem(param));
}

Another idea that I could try is implementing a message service我可以尝试的另一个想法是实现消息服务

I would suggest you do it from HTML by using the target attribute:我建议您使用 target 属性从 HTML 中执行此操作:

<a target="_blank" [routerLink]="['/detail',item.name]">

In this example "/item/:name" should be defined in your routing module.在这个例子中,“/item/:name”应该在你的路由模块中定义。

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

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