1

我有一个 Web 组件库(自定义元素),我希望在其他 Angular 应用程序中使用它。问题是@ViewChildren()获取未定义的值,无法访问我的组件内的子项。

我使用@ViewChildren是因为我正在使用encapsulation: ViewEncapsulation.ShadowDom. 我已经尝试使用@ContentChildren并且它可以工作,但是一旦我在另一个应用程序中使用我的组件,@ContentChildren就不再工作了(由于 ShadowDom)。

这是一个最小的代码片段,只是为了显示手头的问题。完整代码可以在这里访问

父组件:

自定义手风琴.component.ts

export class CustomAccordionComponent implements AfterViewInit {

  @ContentChildren(CustomAccordionContentComponent) contentChildren: QueryList<CustomAccordionContentComponent>;
  @ViewChildren(CustomAccordionContentComponent) viewChildren: QueryList<CustomAccordionContentComponent>;

  setOpen() { this.open = true; }

  toggle() {
    console.log("toggle from accordion called...");
    this.setOpen();
  }

  constructor(public elementRef: ElementRef, private renderer2: Renderer2) { }

  ngAfterViewInit(): void {
    console.log("Content children:", this.contentChildren);
    console.log("View children:", this.viewChildren);
    }
}

custom-accordion.component.html

注意:如果我使用

<div>
   <app-custom-accordion-content></app-custom-accordion-content>
</div>

我可以看到@VIewChildren,但没有显示数据。所以我使用下面的代码来查看显示的数据。

<div>
    <ng-content></ng-content>
</div>

子组件

自定义手风琴内容.component.ts

export class CustomAccordionContentComponent implements OnInit {
  private _contentId: number;
  private _header: string;
  private _opened: boolean = false;
  private _clickFocus: boolean = true;
  private _hideHR: boolean = false;

  @Input() isopen: boolean;

  @Input() set contentId(value: number) { this._contentId = value; }
  get contentId(): number { return this._contentId; }

  @Input('header') set header(value: string) { this._header = value; }
  get header(): string { return this._header; }

  @Output() toggleAccordion: EventEmitter<boolean> = new EventEmitter();

  toggleOpen($event: any) {
    this.opened = !this.opened;
    $event.preventDefault();
    this.toggleAccordion.emit(this.opened);
  }

  keyUp($event: KeyboardEvent) {
    $event.preventDefault();
    if ($event.keyCode === 32 || $event.keyCode === 19) {
      this.toggleAccordion.emit(this.opened);
    }
  }

  // other SETTERS AND GETTERS of private variables

  constructor(public elementRef: ElementRef) { }

  ngOnInit() { }
}

自定义手风琴内容.component.html

<div #accordion class="accordion" [class.selected]="opened" [class.click-focus]="clickFocus" tabindex="{{contentId}}"
  (keyup)="keyUp($event)" (click)="toggleOpen($event)">
  <div class="accordion-panel add-padding row">
    <div class="header col-10">
      {{header}}
    </div>
    <div class="chevron col-2">
      <svg class="chevron" width="17px" height="9.5px" viewBox="0 0 17 10" version="1.1"
        xmlns="http://www.w3.org/2000/svg">
        <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
          <polyline id="Path" stroke="#231F20" stroke-width="2" points="1 1 8.47875354 8.5 15.9575071 1">
          </polyline>
        </g>
      </svg>
    </div>
  </div>
  <div *ngIf="opened" class="accordion-content">
    <ng-content></ng-content>
  </div>
  <hr *ngIf="!hideHR" [class.extend]="opened">
</div>

然后我有包含数据的容器

container.component.html

<div class="form-group w-100">
    <app-custom-accordion>
        <app-custom-accordion-content *ngFor="let content of fullContent; let i = index" [header]="content.header"
            [isopen]="content.open" contentId="{{i}}">
            <div>
                <app-custom-p row="1" col="12" *ngFor="let detail of content.content[0].details" [text]="detail">
                </app-custom-p>
                <app-custom-p row="2" col="12" [text]="content.content[0].footer"></app-custom-p>
            </div>
        </app-custom-accordion-content>
    </app-custom-accordion>
</div>

我一直在尝试解决这个问题,但无济于事。我只想获取@ContentChildren 通过@ViewChildren 获得的值。任何帮助,将不胜感激。

4

0 回答 0