繁体   English   中英

包含组件上的奇怪行为Angular 6

[英]Strange behavior Angular 6 on include components

我购买了一个主题,该主题在HTML + CSS简单项目方法上运行良好,但我想将其转换为Angular 6项目。

我设法使主题在Angular 6上正常工作,但是当在angular 6应用程序中查看我的代码时,我得到了奇怪的CSS行为:

<!-- begin:: Page -->
<div class="m-grid m-grid--hor m-grid--root m-page">

  <m-header *ngIf="showContent"></m-header>

  <!-- begin::Body -->
  <div class="m-grid__item m-grid__item--fluid m-grid m-grid--ver-desktop m-grid--desktop m-body">

    <m-leftmenu></m-leftmenu>

    <div *ngIf="showContent" #mContentWrapper>

      <router-outlet></router-outlet>

    </div>

  </div>

  <m-footer></m-footer>

</div>
<!-- end:: Page -->

<!-- begin::Scroll Top -->
<div id="m_scroll_top" class="m-scroll-top">
  <i class="la la-arrow-up"></i>
</div>

<!-- end::Scroll Top -->

这是我的PainelComponent(painel.component.html)中的代码,我使用带有延迟加载的来将我的HomeComponent加载到PainelComponent内部,这是HomeComponent的代码:

<div class="m-grid__item m-grid__item--fluid m-wrapper">

    <!-- BEGIN: Subheader -->
    <div class="m-subheader ">
      <div class="d-flex align-items-center">
        <div class="mr-auto">
          <h3 class="m-subheader__title ">Painel</h3>
        </div>
        <div>
          <span class="m-subheader__daterange" id="m_dashboard_daterangepicker">
            <span class="m-subheader__daterange-label">
              <span class="m-subheader__daterange-title"></span>
              <span class="m-subheader__daterange-date m--font-brand"></span>
            </span>
            <a href="#" class="btn btn-sm btn-brand m-btn m-btn--icon m-btn--icon-only m-btn--custom m-btn--pill">
              <i class="la la-angle-down"></i>
            </a>
          </span>
        </div>
      </div>
    </div>

    <!-- END: Subheader -->
    <div class="m-content">

      <!--Begin::Section-->
      <div class="row">
        <div class="col-xl-12">

        </div>

        <div class="col-xl-12 col-lg-12">

          <!--begin:: Widgets/Quick Stats-->
          <div class="row m-row--full-height">
            <div class="col-sm-12 col-md-12 col-lg-6">
              <div class="m-portlet m-portlet--half-height m-portlet--border-bottom-brand ">
                <div class="m-portlet__body">
                  <div class="m-widget26">
                    <div class="m-widget26__number">
                      570
                      <small>All Sales</small>
                    </div>
                    <div class="m-widget26__chart" style="height:90px; width: 220px;">
                      <canvas id="m_chart_quick_stats_1"></canvas>
                    </div>
                  </div>
                </div>
              </div>
              <div class="m--space-30"></div>
              <div class="m-portlet m-portlet--half-height m-portlet--border-bottom-danger ">
                <div class="m-portlet__body">
                  <div class="m-widget26">
                    <div class="m-widget26__number">
                      690
                      <small>All Orders</small>
                    </div>
                    <div class="m-widget26__chart" style="height:90px; width: 220px;">
                      <canvas id="m_chart_quick_stats_2"></canvas>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div class="col-sm-12 col-md-12 col-lg-6">
              <div class="m-portlet m-portlet--half-height m-portlet--border-bottom-success ">
                <div class="m-portlet__body">
                  <div class="m-widget26">
                    <div class="m-widget26__number">
                      230
                      <small>All Transactions</small>
                    </div>
                    <div class="m-widget26__chart" style="height:90px; width: 220px;">
                      <canvas id="m_chart_quick_stats_3"></canvas>
                    </div>
                  </div>
                </div>
              </div>
              <div class="m--space-30"></div>
              <div class="m-portlet m-portlet--half-height m-portlet--border-bottom-accent ">
                <div class="m-portlet__body">
                  <div class="m-widget26">
                    <div class="m-widget26__number">
                      470
                      <small>All Comissions</small>
                    </div>
                    <div class="m-widget26__chart" style="height:90px; width: 220px;">
                      <canvas id="m_chart_quick_stats_4"></canvas>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <!--end:: Widgets/Quick Stats-->
        </div>

      </div>
      <!--End::Section-->


    </div>
  </div>

这是我不想要的方法(这与ANGULAR 6有关)

我不想的风格

但是,当我从HomeComponent中选择所有代码时,请从PainelComponent中删除以下行:

<div *ngIf="showContent" #mContentWrapper>

      <router-outlet></router-outlet>

    </div>

并将HomeComponent中的所有代码与我期望的100%全角包装器一起使用,为什么呢? 我在使用路由器时必须做的事情是,我以为Angular只是从组件中添加HTML文件,但我想不是这样的。

这是我将home.component.html放入painel.component.html内时的代码

<!-- begin:: Page -->
<div class="m-grid m-grid--hor m-grid--root m-page">

  <m-header *ngIf="showContent"></m-header>

  <!-- begin::Body -->
  <div class="m-grid__item m-grid__item--fluid m-grid m-grid--ver-desktop m-grid--desktop m-body">

    <m-leftmenu></m-leftmenu>


  <div class="m-grid__item m-grid__item--fluid m-wrapper">

    <!-- BEGIN: Subheader -->
    <div class="m-subheader ">
      <div class="d-flex align-items-center">
        <div class="mr-auto">
          <h3 class="m-subheader__title ">Painel</h3>
        </div>
        <div>
          <span class="m-subheader__daterange" id="m_dashboard_daterangepicker">
            <span class="m-subheader__daterange-label">
              <span class="m-subheader__daterange-title"></span>
              <span class="m-subheader__daterange-date m--font-brand"></span>
            </span>
            <a href="#" class="btn btn-sm btn-brand m-btn m-btn--icon m-btn--icon-only m-btn--custom m-btn--pill">
              <i class="la la-angle-down"></i>
            </a>
          </span>
        </div>
      </div>
    </div>

    <!-- END: Subheader -->
    <div class="m-content">

      <!--Begin::Section-->
      <div class="row">
        <div class="col-xl-12">

        </div>

        <div class="col-xl-12 col-lg-12">

          <!--begin:: Widgets/Quick Stats-->
          <div class="row m-row--full-height">
            <div class="col-sm-12 col-md-12 col-lg-6">
              <div class="m-portlet m-portlet--half-height m-portlet--border-bottom-brand ">
                <div class="m-portlet__body">
                  <div class="m-widget26">
                    <div class="m-widget26__number">
                      570
                      <small>All Sales</small>
                    </div>
                    <div class="m-widget26__chart" style="height:90px; width: 220px;">
                      <canvas id="m_chart_quick_stats_1"></canvas>
                    </div>
                  </div>
                </div>
              </div>
              <div class="m--space-30"></div>
              <div class="m-portlet m-portlet--half-height m-portlet--border-bottom-danger ">
                <div class="m-portlet__body">
                  <div class="m-widget26">
                    <div class="m-widget26__number">
                      690
                      <small>All Orders</small>
                    </div>
                    <div class="m-widget26__chart" style="height:90px; width: 220px;">
                      <canvas id="m_chart_quick_stats_2"></canvas>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div class="col-sm-12 col-md-12 col-lg-6">
              <div class="m-portlet m-portlet--half-height m-portlet--border-bottom-success ">
                <div class="m-portlet__body">
                  <div class="m-widget26">
                    <div class="m-widget26__number">
                      230
                      <small>All Transactions</small>
                    </div>
                    <div class="m-widget26__chart" style="height:90px; width: 220px;">
                      <canvas id="m_chart_quick_stats_3"></canvas>
                    </div>
                  </div>
                </div>
              </div>
              <div class="m--space-30"></div>
              <div class="m-portlet m-portlet--half-height m-portlet--border-bottom-accent ">
                <div class="m-portlet__body">
                  <div class="m-widget26">
                    <div class="m-widget26__number">
                      470
                      <small>All Comissions</small>
                    </div>
                    <div class="m-widget26__chart" style="height:90px; width: 220px;">
                      <canvas id="m_chart_quick_stats_4"></canvas>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <!--end:: Widgets/Quick Stats-->
        </div>

      </div>
      <!--End::Section-->


    </div>
  </div>


  </div>

  <m-footer></m-footer>

</div>
<!-- end:: Page -->

<!-- begin::Scroll Top -->
<div id="m_scroll_top" class="m-scroll-top">
  <i class="la la-arrow-up"></i>
</div>

<!-- end::Scroll Top -->

这是我想要的方法

我想要的风格

Angular使用shadow-dom浏览器的功能来渲染您的组件(使用ViewEncapsulation)。

如果您注意的话,您应该注意到使用<m-home>在html树中添加了一个节点。

一个简单的解决方案是:

  • 在你home.component.ts ,改变你的选择器div[m-home]的insteaad m-home
  • 在您的home.component.html删除第一个<div> (并确保删除末尾的括号)。
  • 现在,在使用<m-home>的模板中,将其替换为: <div m-home class="m-grid__item m-grid__item--fluid m-wrapper"> </div>

再次检查您的html树,您会注意到其中的区别

暂无
暂无

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

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