【问题标题】:Ionic 2 re-usable page componentIonic 2 可重用的页面组件
【发布时间】:2018-10-24 20:18:10
【问题描述】:

我正试图弄清楚如何创建一个可重用的页面组件,但 ionic 有很多嵌入错误。

我已经尝试制作一个页面列表库:

<ion-header *ngIf="header || hasHeader">
    <ion-navbar>
        <ng-container  *ngTemplateOutlet="header"></ng-container>
    </ion-navbar>
</ion-header>
<ion-content>
   <!-- TODO: abstract refresher once it can be put in a component without ion content -->
   <ion-refresher (ionRefresh)="pageListBase.refresh($event)">
     <ion-refresher-content pullingIcon="arrow-dropdown" pullingText="Pull to refresh"
                          refreshingSpinner="circles" refreshingText="Refreshing...">
     </ion-refresher-content>
   </ion-refresher>
   <list-base #pageListBase [filter]="filter" 
                            [baseProvider]="baseProvider" 
                            [config]="config" 
                            [template]="itemTemplate">
   </list-base>
   <ion-footer *ngIf="footer">
       <ng-container *ngTemplateOutlet="footer"></ng-container>    
   </ion-footer>
 </ion-content>

我在这里重复使用这个页面:

<page-base [header]="headerTemplate"></page-base>

<ng-template #headerTemplate>
  <ion-header>
    <ion-navbar align-title="center">
      <ion-buttons start>
          <button ion-button>Save</button>
      </ion-buttons>
      <ion-searchbar></ion-searchbar>
    </ion-navbar>
  </ion-header>
</ng-template>

还有这里:

<page-base #pageBase [header]="headerTemplate"></page-base>

<ng-template #headerTemplate>
  <ion-header>
    <ion-navbar align-title="center">
      <ion-buttons start>
        <button ion-button>button</button>
      </ion-buttons>
      <ion-title start>
          Back Button is hidden behind the header
      </ion-title>
    </ion-navbar>
  </ion-header>
</ng-template>

我做了一个repo to express the problem Here

复制: 点击头像头像,

Bug:出现标题模板但返回按钮绑定到父标题。

尝试修复

1.如果您从外部删除Ion-Header,则会产生Margin Bug。

2.如果你把Ion-Header从里面去掉,ion-buttons的样式就会改变

我不想使用 css 修复的兔子洞。 如何在不破坏 ionic 布局或功能的情况下创建可重用的 ionic 页面组件?

【问题讨论】:

    标签: javascript html angular typescript ionic-framework


    【解决方案1】:

    在我看来,问题在于您实际上渲染了两次 &lt;ion-header&gt; 和 &lt;ion-navbar&gt;:您将它们放在 page-base 模板中,其中 ng-container 包含另一对 &lt;ion-header&gt; 和 &lt;ion-navbar&gt;。

    所以,我做了什么并且工作得很好:

    page-base.html:

    改变:

    <ion-header>
      <ion-navbar align-title="center">
        <ng-container *ngTemplateOutlet="header"></ng-container>
      </ion-navbar>
    </ion-header>
    

    到:

    <ng-container *ngTemplateOutlet="header"></ng-container>
    

    【讨论】:

    • 请阅读完整的问题,正如我所说的“2。如果你从内部移除离子头,离子按钮的样式会改变”这样做会改变标题中按钮的样式即将按钮呈现为具有不同样式的块。有关示例,请参见我的仓库
    • 我尝试了你的代码并根据我的回答进行了更改,我发现标题完全没有区别(右侧的搜索栏+保存按钮,白色背景,黑色文本),并且出现后退按钮正如预期的那样。但是,如果我从 about.html 中删除 ion-header 和 ion-navbar 标签,那么样式确实会发生变化。我不知道确切的原因,这可能是一个错误..
    • 如果标题样式很好,则意味着这是另一个错误,并且第一项隐藏在标题后面,因为 ion-content 不尊重标题。
    猜你喜欢
    • 2019-03-04
    • 2016-06-15
    • 2017-02-11
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多