简体   繁体   English

如何在jsPanel Angular2中将组件加载为内容

[英]How to load component as content in jsPanel Angular2

I am trying to load an Angular component in jsPanel, but it doesn't work yet.我正在尝试在 jsPanel 中加载一个 Angular 组件,但它还不起作用。 Chrome DevTools always show <app-chat-tab></app-chat-tab> instead of the component html, so I think it's not rendering at all. Chrome DevTools 总是显示<app-chat-tab></app-chat-tab>而不是组件 html,所以我认为它根本没有渲染。

This is my code:这是我的代码:

export class ChatTabService {
  jsPanel: any;
  activeChats: ClientUser[];

  constructor(@Inject(PLATFORM_ID) private platformId, private zone: NgZone) {
    if (isPlatformBrowser(this.platformId)) {
      this.jsPanel = require('node_modules/jspanel4/es6module/jspanel.min.js').jsPanel;
    }
  }

  openChatWindow(toUser: ClientUser) {
    if (!this.activeChats.find(u => u.id === toUser.id)) {
        this.jsPanel.create({
          theme: 'primary',
          headerTitle: toUser.username,
          position: 'center-top 0 58',
          contentSize: '450 250',
          content: '<app-chat-tab></app-chat-tab>',
          callback() {
            this.content.style.padding = '20px';
          },
          onbeforeclose() {
            return confirm('Do you really want to close the panel?');
          }
        });
        this.activeChats.push(toUser);
    }
  }

}

Any ideas?有任何想法吗?

Yes you can use Angular component in jsPanel content.是的,您可以在 jsPanel 内容中使用 Angular 组件。 For that you have to first get the nativeElemetn of the component that you want to use inside content of jsPanel.为此,您必须首先获取要在 jsPanel 的内容中使用的组件的 nativeElemetn。

So if your component name is "PopupContentComponent" then you need to add below code.因此,如果您的组件名称是“PopupContentComponent”,那么您需要添加以下代码。

const factory = this.resolver.resolveComponentFactory(PopupContentComponent);
const componentRef = this.vcr.createComponent(factory);
const element = componentRef.location.nativeElement;

Be sure that you have included the dependencies in the constructive.确保您已将依赖项包含在构造函数中。

constructor(private cd: ChangeDetectorRef,
        private vcr: ViewContainerRef,
        private resolver: ComponentFactoryResolver) {
    }

And then you can user "element" inside the jsPanel "content".然后您可以在 jsPanel“内容”中使用“元素”。

onwindowresize: true,
content: element,
onstatuschange: (panel, status) => {

This is tested with Angular 10.这是用 Angular 10 测试的。

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

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