javascript - 如何解决 Angular 8 中的 View 封装问题?

标签 javascript angular

我有父组件和子组件。作为模态组件创建的子组件。所以我在父组件中包含了子组件选择器,并且我设置了 View 封装为无,因此它将采用父组件 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);
}

父组件 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>

子组件 HTML
<div class="app-modal">
    <div class="app-modal-body">
        <ng-content></ng-content>
    </div>
</div>
<div class="app-modal-background"></div>

子组件 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');
  }

}

服务代码
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();
  }
}

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

最佳答案

因为你的模态。
模态创建于 顶部 您的应用程序,(如果您通过检查查找 模态包装器 ,则超出正文标记。)

是 Modal 导致 dom 结构与 ng 组件结构不同步

那么为什么模态与 ng Encapsulation 一起使用?因为您的模态服务可以处理它。

但是第 3 方 CSS 文件不是您的 Angular 编译配置的一部分(即只有 sass、scss 有效)。所以它确实忽略了任何 .css即使您正确导入它。

要解决它,您可以全局应用 .css。或者,如果您对此感到害怕 覆盖 其他全局样式,您可以将 css 文件重命名为 '_somefile.scss'(注意破折号 '_',这很重要。)

然后在您的项目全局 style.scss 文件下>创建一个与您的模态包装器匹配的选择器>在选择器下:添加一行 @import somefile (不要添加扩展名)

样式.scss

.modal-wrapper {
    @import somepath/somefile
}

关于javascript - 如何解决 Angular 8 中的 View 封装问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60505466/

相关文章:

angular - 强制 mat-expansion-panel 溢出容器元素

javascript - React native 无法将读取权限(电子邮件)传递给发布授权请求

javascript - Angular 2 是取代普通的旧 AngularJS 还是替代品?

c# - 如何使用 Web api 和 Angular 从字节下载文件

javascript - 更新子级的 @Input 属性而不更新父级

通过 *ngFor 在数组中以 Angular 显示对象

javascript - 游戏的每回合超时逻辑

javascript - HTML 表格转 Excel,支持跨浏览器

javascript - 让函数在获取和返回 DOM 元素之前等待

angular - combineLatest 抛出 TypeError