简体   繁体   English

打开一个新的 window 无需重启 angular 应用程序

[英]Open a new window without restart angular application

I'm trying to open a new window on angular with some informations passing the values in the route to call the endpoint, but when i did this, all the angular application is calling again... This cost me a several times only to show a simple html page.我正在尝试在 angular 上打开一个新的 window ,其中一些信息通过路由中的值来调用端点,但是当我这样做时,所有的 angular 只显示几个调用一个简单的 html 页面。 There is another way to do this?还有另一种方法吗?

You could try to use Angular CdkPortal to create new portals (or windows).您可以尝试使用 Angular CdkPortal创建新的门户(或窗口)。 A simple portal would look like一个简单的门户看起来像

import {Component, ViewChild, OnInit, ComponentFactoryResolver, ApplicationRef, Injector, OnDestroy } from '@angular/core';
import {CdkPortal, DomPortalHost} from '@angular/cdk/portal';

@Component({
  selector: 'app-window',
  template: `
    <ng-container *cdkPortal>
      <ng-content></ng-content>
    </ng-container>
  `
})
export class WindowComponent implements OnInit, OnDestroy {
  @ViewChild(CdkPortal) portal: CdkPortal;
  private externalWindow = null;

  constructor(
    private componentFactoryResolver: ComponentFactoryResolver,
    private applicationRef: ApplicationRef,
    private injector: Injector){}

  ngOnInit(){
    this.externalWindow = window.open('', '', 'width=600,height=400,left=200,top=200');

    const host = new DomPortalHost(
      this.externalWindow.document.body,
      this.componentFactoryResolver,
      this.applicationRef,
      this.injector
      );

    host.attach(this.portal);
  }

  ngOnDestroy(){
    this.externalWindow.close()
  }
}

Any information passed between the <app-window> tags will be sent to the WindowComponent with the help of <ng-content> . <app-window>标签之间传递的任何信息都将在<ng-content>的帮助下发送到WindowComponent For eg.例如。

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `
    <p>Click this button to open a new window:</p>
    <button (click)="this.showPortal = true">Open Window</button>

    <app-window *ngIf="showPortal">
      <h2>Data from another window.</h2>
      <button (click)="this.showPortal = false">Close Window</button>
    </app-window>
  `,
})
export class AppComponent  {
  showPortal = false;
}

Working example: Stackblitz工作示例: Stackblitz

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

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