首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PrimeNg TabView动态组件

PrimeNg TabView动态组件
EN

Stack Overflow用户
提问于 2021-02-06 16:51:46
回答 1查看 1.4K关注 0票数 0

我遵循方法,基于配置JSON在AngularApp中动态创建元素。不幸的是,这似乎并不适用于所有的PrimeNG组件。尤其是我也不想使用的TabView。

我对棱角不是很熟悉,但已经试了一整天了。在我看来,根本的问题与TabView如何识别DOM中继承的TabPanels有关,但不幸的是,这不是我可以改变的东西--我可以吗?

另外,似乎有一个与此相关的GitHub问题,但最终他们编写了一个自定义选项卡组件。有人看到绕过这条路了吗?它可以很好地工作与其他对象/元素的总理堆栈,只是表视图的行为有点奇怪.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-07 14:01:44

我解决了这个问题。基本上,它将停止CreateDynamicComponentService在TabView组件中的递归foreach循环,在TabView中设置一个制表符变量,并从服务中重新启动递归函数:

create-dynamic-component.service

代码语言:javascript
运行
复制
createComponent(content: any, type: any, vcRef) {
    const componentRef = this.renderComp(content, type, vcRef)
    if (content.nodes && content.nodes.length) {
      if (!componentRef.instance.embeddedContainer) {
        const cmpName = componentRef.instance.constructor.name;
        if (cmpName != 'TabViewComponent') {
          throw new TypeError('Trying to render embedded content. ${cmpName} must have @ViewChild() embeddedContainer defined');
        }
      } else {
        content.nodes.forEach(type => {
          const typeP = this.contentMappings[type.type];
          this.createComponent(type, typeP, componentRef.instance.embeddedContainer);
        });
      }
    }
  }

TabViewComponent

代码语言:javascript
运行
复制
@Component({
  selector: 'dynamic-page-tab-view',
  template: '<p-tabView dynamic="true" cache="false">
                <p-tabPanel [header]="tab.header" *ngFor="let tab of tabs; let i = index" [selected]="i == 0">
                {{ tab.header }}
                    <ng-container #panel></ng-container>
                </p-tabPanel>
            </p-tabView>'
})
export class TabViewComponent implements AfterViewInit {
  @ViewChild('container', { read: ViewContainerRef,  static: true })
  embeddedContainer: ViewContainerRef;

  @ViewChildren('panel', {read: ViewContainerRef}) tabPanels: QueryList<ViewContainerRef>

  activeIndex: number;
  tabs: any = []

  constructor(private changeDetector : ChangeDetectorRef,
              private viewContainerRef: ViewContainerRef,
              private createDynamicComponentService: CreateDynamicComponentService,
              @Inject(CONTENT_MAPPINGS) private contentMappings: any) { }

  contentOnCreate(values: { [key: string]: any; }): void {
    this.tabs = values["nodes"];
    this.activeIndex = 0;
  }

  ngAfterViewInit() {
    this.tabPanels.forEach((viewRef: ViewContainerRef, index: number) => {
      console.log(index, viewRef, this.tabs[index]);
      const type = this.tabs[index];
      const typeP = this.contentMappings[type.type];
      this.createDynamicComponentService.createComponent(type, typeP, viewRef);
      this.changeDetector.detectChanges();
    });
  }

}

重要的是,TabPanel组件只存在于单个容器中。

代码语言:javascript
运行
复制
<ng-container #container></ng-container>

PrimeNg TabPanel本身在TabView组件中创建和模板化。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66079407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档