如何解决 Angular8 中的视图封装问题?

bag*_*gya 8 javascript angular

我有父组件和子组件。创建为模态组件的子组件。所以我在父组件中包含了子组件选择器,并且我设置了视图封装为 none 以便它将采用父组件 css 和所有它也可以工作但父组件有 #paper id 应用一些第三方(rappidjs)库 css(用于SVG 图表)。就像子组件有 #dataMapper id 一样。但在这里第三方 css 没有使用,因为子组件设置为'encapsulation: ViewEncapsulation.None'. 如果我将删除encapsulation: ViewEncapsulation.None它的工作,但模态不起作用。所有模态加载而不是onclick。如何解决这个问题请给我建议。

编码:

父组件 TS

@Component({
  selector: 'app-data-model',
  templateUrl: './data-model.component.html',
  styleUrls: ['./data-model.component.css']
})

export class DataModelComponent implements OnInit{

// Modal

openModal(id: string) {
  this.modalApp = true;
  this.modalService.open(id);
}

closeModal(id: string) {
  this.modalService.close(id);
}
Run Code Online (Sandbox Code Playgroud)

父组件 HTML

<div id="toolbar">
    <div class="tool-bar-section">
    <button class="btn" (click)="openModal('custom-modal-1');"><i class="fa fa-file-excel-o" style="font-size: 24px"></i></button>     
    </div>   
</div>
<div id="paper"> </div> ( this dom taking thirdparty css)

<app-modal id="custom-modal-1">           
    <h1 class="head-bar">Data Model - Import Excel  <a href="#" class="close-icon" (click)="closeModal('custom-modal-1');"><i class="fa fa-times-circle-o" aria-hidden="true"></i></a></h1>
    <div class="modal-content-section">
     <ul>
         <li><a href="#" (click)="closeModal('custom-modal-1');openModal('custom-modal-2');">Create New Schema</a></li>
         <li><a href="#" (click)="closeModal('custom-modal-1');openModal('custom-modal-3');">Import Data to existing schema</a></li>
     </ul>       
    </div>        
</app-modal>
<app-modal id="custom-modal-2">       
    <h1 class="head-bar">Data Model - Import Excel  <a href="#" class="close-icon" (click)="closeModal('custom-modal-2');"><i class="fa fa-times-circle-o" aria-hidden="true"></i></a></h1>
    <div class="modal-content-section">
        <div id="dataMapper"></div>       ( this dom is not taking thirdparty css)
        <p><a href="#" (click)="closeModal('custom-modal-2');openModal('custom-modal-4');">Data Mapping</a></p>
    </div>
</app-modal>
Run Code Online (Sandbox Code Playgroud)

子组件 HTML

<div class="app-modal">
    <div class="app-modal-body">
        <ng-content></ng-content>
    </div>
</div>
<div class="app-modal-background"></div>
Run Code Online (Sandbox Code Playgroud)

子组件 Ts

@Component({
  selector: 'app-modal',
  templateUrl: './modal.component.html',
  styleUrls: ['./modal.component.css'],
  encapsulation: ViewEncapsulation.None
})
export class ModalComponent implements OnInit, OnDestroy {

  @Input() id: string;
  private element: any;

  constructor(private modalService: ModalService, private el: ElementRef) {
      this.element = el.nativeElement;
  }

  ngOnInit(): void {
      // ensure id attribute exists
      if (!this.id) {
          console.error('modal must have an id');
          return;
      }

      // move element to bottom of page (just before </body>) so it can be displayed above everything else
      document.body.appendChild(this.element);

      // close modal on background click
      this.element.addEventListener('click', el => {
          if (el.target.className === 'app-modal') {
              this.close();
          }
      });

      // add self (this modal instance) to the modal service so it's accessible from controllers
      this.modalService.add(this);
  }

  // remove self from modal service when component is destroyed
  ngOnDestroy(): void {
      this.modalService.remove(this.id);
      this.element.remove();
  }

  // open modal
  open(): void {
      this.element.style.display = 'block';
      document.body.classList.add('app-modal-open');
  }

  // close modal
  close(): void {
      this.element.style.display = 'none';
      document.body.classList.remove('app-modal-open');
  }

}
Run Code Online (Sandbox Code Playgroud)

模态服务代码

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

@Injectable({
  providedIn: 'root'
})
export class ModalService {

  private modals: any[] = [];

  add(modal: any) {
      // add modal to array of active modals
      this.modals.push(modal);
  }

  remove(id: string) {
      // remove modal from array of active modals
      this.modals = this.modals.filter(x => x.id !== id);
  }

  open(id: string) {
      // open modal specified by id
      const modal = this.modals.find(x => x.id === id);
      modal.open();
  }

  close(id: string) {
      // close modal specified by id
      const modal = this.modals.find(x => x.id === id);
      modal.close();
  }
}
Run Code Online (Sandbox Code Playgroud)

如果需要更多详细信息,请告诉我。

Jas*_*ngh 0

正如您在问题中所描述的,您希望在子组件中继承父组件的CSS。所以,我假设您在父组件和子组件中具有相同的 HTML 元素,并且您不想重复您的 css。

由于我们无法复制您的问题,并且您尚未创建任何 stackblitz 实例。我会建议一个更好的可行的替代方案。请在下面找到它:

styleUrls我们可以在装饰器元数据对象的属性中指定多个CSS URL @Component。因此,我建议您也传递父样式文件 Url。

我创建了一个基本的 stackblitz,显示我在哪里访问父组件的 CSS: https: //stackblitz.com/edit/angular-jhewzy

父组件 css 文件 (app.component.css) - 我指定p父组件内标签的背景颜色应为黄色。我想在子组件中继承它。

p {
  background-color: yellow;
}
Run Code Online (Sandbox Code Playgroud)

下面是子组件 CSS (hello.component.css)

p {
  font-style: italic;
}
Run Code Online (Sandbox Code Playgroud)

当您想在子组件中使用父组件时,只需在 styleUrls 属性中使用样式 URL 路径

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

@Component({
  selector: 'hello',
  template: `<p>I am child</p>`,
  styleUrls: [`./hello.component.css`, './app.component.css'],
})


export class HelloComponent  {
  @Input() name: string;
}
Run Code Online (Sandbox Code Playgroud)

我希望它能帮助你。