Ionic 4如何接收componentProps?

ntg*_*ner 5 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();
}
Run Code Online (Sandbox Code Playgroud)

我的问题是;在实际的模态中,如何test: "123"进入变量?

Ser*_*ero 10

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

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();
  }
}
Run Code Online (Sandbox Code Playgroud)

在带有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);
  }
}
Run Code Online (Sandbox Code Playgroud)


Tah*_*shi 6

您还可以使用 Navparams 来获取 componentProps 的值。

import { CommentModalPage } from '../comment-modal/comment-modal.page';
import { ModalController, IonContent } from '@ionic/angular';


constructor(public modalCtrl : ModalController) {  }

  async commentModal() {
      const modal = await this.modalCtrl.create({
        component: CommentModalPage,

        componentProps: { value: 'data'}
      });
      return await modal.present();
   }
Run Code Online (Sandbox Code Playgroud)

在您的 commentModalPage 中,您只需导入 navprams 并从中获取值。

import { NavParams} from '@ionic/angular';

constructor(public navParams : NavParams) {  

              console.log(this.navParams.get('value'));

            }
Run Code Online (Sandbox Code Playgroud)