ionic-framework - Ionic 4如何接收componentProps?

标签 ionic-framework ionic4

在Ionic 4中,我试图使用modalController打开模式。我可以打开模式并发送componentProps,但是我不确定如何接收这些属性。

这是我打开模态组件的方法:

async showUpsert() {
  this.modal = await this.modalController.create({
    component:UpsertComponent,
    componentProps: {test: "123"}
  });
  return await this.modal.present();
}


我的问题是;在实际模态中,如何将test: "123"转换为变量?

最佳答案

您可以在需要的组件中使用“输入组件交互”来获取这些值,例如:

import { Component } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { TestComponent } from '../test/test.component';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss']
})
export class HomePage {
  constructor(public modalController: ModalController){}
  async presentModal() {
    const modal = await this.modalController.create({
      component: TestComponent,
      componentProps: { value: 123, otherValue: 234 }
    });
    return await modal.present();
  }
}


在带有Input的模态组件中,您可以采用以下参数:

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

@Component({
  selector: 'app-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.scss']
})
export class TestComponent implements OnInit {
  @Input("value") value;
  @Input() otherValue;
  constructor() { }

  ngOnInit() {
    //print 123
    console.log(this.value);
    //print 234
    console.log(this.otherValue);
  }
}

关于ionic-framework - Ionic 4如何接收componentProps?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51989882/

相关文章:

android - ionic 自定义 oauth2 重定向 URL Angular

ionic-framework - 我可以将 ionic serve 设置为默认不打开浏览器吗?

javascript - 从本地json获取带有id的单个数据

ionic-framework - 如何在 Ionic 4 中更改工具栏颜色

android - 在 Ionic 4 中处理后退按钮的问题

android - 从 Android Intent 打开 Ionic (Capacitor) 应用程序中的特定页面

ionic-framework - 从商店取消发布应用之前的“待办事项”列表

ionic-framework - 如何在ionic 4中实现“后退”按钮功能?

angular - 错误 : Cannot match any routes. URL 段: 'tabs'

angularjs - 关于使用 ionic 框架在本地保存数据的任何想法