【问题标题】:Ionic: How do I include header from one page when I navigate to another page?Ionic:当我导航到另一个页面时,如何包含一个页面的标题?
【发布时间】:2019-11-06 21:26:53
【问题描述】:

我正在使用 Ionic 3,在我的主页上,我有以下代码。当我单击 searchMore 按钮时,它会导航到一个新页面。我想在新页面的 h5 中包含“Cook With What is in My Pantry”。

  <ion-grid>
  <ion-row>
    <ion-col col-9>
      <h5 name="cookWith">Cook With What is in My Pantry</h5>
    </ion-col>
    <div class="applyMore">
      <ion-col col-3>
        <button (click)="searchMore()" ion-button clear icon-left>
          More
        <ion-icon name='arrow-forward'></ion-icon>
        </button>
      </ion-col>
    </div>
  </ion-row>
</ion-grid>

在它链接到的页面上,我希望 h5 出现在以下代码中。

<h5 name="cookWith">{{cookWith}}</h5>

在我的 .TS 文件中,我有以下内容

从首页

  export class HomePage {constructor(public navCtrl: NavController, 
  public navParams:          
  NavParams) {

   }

  /**
  *links filter button to FilterPage
  */

 recipeFilter(event, button) {
   this.navCtrl.push(FilterPage, {
   button: button
   });
   }

  searchMore(event, button) {
    this.navCtrl.push(RecipesearchPage, {
    });
    ;
   }
  }

它链接到的页面

export class RecipesearchPage {

 constructor(public navCtrl: NavController, public navParams: 
 NavParams) {   
 } 

 ionViewDidLoad() {
  console.log('ionViewDidLoad RecipesearchPage');
 }

 /**
 *on click, links from recipe card to individual recipe page
 */
 individualRecipe() {
  this.navCtrl.push(RecipePage);
  };

}

【问题讨论】:

    标签: angular ionic3


    【解决方案1】:

    所以我认为你可以这样做。如果将来需要更改字符串,可以在构造函数中更改它。我认为如果它是静态的并且不会改变,为什么不在两个地方都有它呢?

    选项#1:如果您以后需要动态更新标签。

    <ion-grid>
      <ion-row>
        <ion-col col-9>
          <h5>{{cookWith}}</h5>
        </ion-col>
        <div class="applyMore">
          <ion-col col-3>
            <button (click)="searchMore()" ion-button clear icon-left>
              More
              <ion-icon name='arrow-forward'></ion-icon>
            </button>
          </ion-col>
        </div>
      </ion-row>
    </ion-grid>
    

    在主组件类上添加值的声明:

    export class HomePage {
      cookWith: string;
    
      constructor(
        public navCtrl: NavController, 
        public navParams: NavParams
      ){
         this.cookWith = 'Cook With What is in My Pantry';
      }
    
      /**
       *links filter button to FilterPage
       */
       recipeFilter(event, button) {
          this.navCtrl.push(FilterPage, { 
             button: button
          });
       }
    
       searchMore(event, button) {
         this.navCtrl.push(RecipesearchPage, {
            cookWith: this.cookWith
         });
       }
    }
    

    然后你在另一边捡起来:

    export class RecipesearchPage {
      cookWith: string;
    
      constructor(
        public navCtrl: NavController, 
        public navParams: NavParams
      ){
        this.cookWith = navParams.get('cookWith');
      } 
    
      ionViewDidLoad() {
        console.log('ionViewDidLoad RecipesearchPage');
      }
    
      /**
       *on click, links from recipe card to individual recipe page
       */
       individualRecipe() {
         this.navCtrl.push(RecipePage);
       };
     }
    

    选项 #2:只需将 html 放在两个位置:

     <h5>Cook With What is in My Pantry</h5>
    

    【讨论】:

    • 我实现了它并得到以下错误:Typescript 错误找不到名称'cookWith'。您的意思是实例成员“this.cookWith”吗? src/pages/recipesearch/recipesearch.ts ){ cookWith = navParams.get('cookWith'); }
    • 当我将 cookWith 更改为 this.cookWith 目标打字稿页面时,错误消失了,但现在从或目标 html 页面中都没有出现 h5
    • 我已经更新了代码,让 this.cookWith 在构造函数中为变量赋值。
    • 嘿,所以我建议再次查看我的解决方案。我更新了导航参数。我传递了 undefined 因为 this.cookWith 没有作为参数传递给 navcontroller 的推送。
    【解决方案2】:

    在 ionic/angular 的多个页面中重用相同标题的最佳方法是通过directive

    【讨论】:

      猜你喜欢
      • 2010-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多