【问题标题】:How to pass data to another page from Input Form in IONIC 5如何将数据从 IONIC 5 中的输入表单传递到另一个页面
【发布时间】:2021-05-18 17:48:55
【问题描述】:

我有一个带有 3 文本输入的输入表单。我唯一想要的就是将这 3 个数据发送到另一个我称为“身份”的页面。我想知道如何使用 ionic 5 将这 3 个数据发送到另一个页面。我想创建一个逐步签名的样式。有人可以帮忙吗?

REGISTER.PAGE.HTML

  <ion-content fullscreen="true" class="background-contact">
    <div class="fixed">
      
      <div class="div-3">
            <div align="center">
              <b>IDENTIFICATION</b>
            </div>
  
            <ion-label position="stacked" class="title">E-mail</ion-label>
            <div align="center" class="input-class">
              <ion-input autofocus="true" ngModel  name="email" type="email" placeholder="informez votre email"></ion-input>
            </div> 
        
            <div class="line">
              <ion-label position="stacked" class="title">Mot de passe</ion-label>
              <div align="center" class="input-class">
                <ion-input ngModel name="password" type="password" placeholder="informez votre mot de passe"></ion-input>
              </div>
            </div>
  
            <!-- <div class="line">
              <ion-label position="stacked" class="title">Téléphone</ion-label>
              <div align="center" class="input-class">
                <ion-input autofocus="true" ngModel name="phone" type="text"  placeholder="Tapez votre numéro de tel" (keypress)="numberOnlyValidation($event)"></ion-input>
              </div>
            </div> -->
  
            <div class="line">
              <ion-label position="stacked" class="title">Pays</ion-label>
              <div align="center" >
                <ion-select  cancelText="Fermer" okText="Confirmer" ngModel name="cod_country">
                  <ion-select-option value="">Dans quel pays êtes-vous?</ion-select-option>
                    <ion-select-option value="{{item?.id}}" *ngFor="let item of country">{{item?.name}}</ion-select-option>
                </ion-select>
              </div>
            </div>
        
            <div class="line">
              <div align="center" class="input-class">
                <ion-button  (click)="goToAboutPage()"  expand="block" color="primary"><ion-spinner *ngIf="loading"></ion-spinner> CONFIRMER <ion-icon name="checkmark-done"></ion-icon></ion-button>
              </div>
            </div>
          </div>
    </div>
  </ion-content>

RESGISTER.PAGE.TS

import { Component, OnInit } from '@angular/core';
import { AlertController, NavController, ToastController } from '@ionic/angular';
import { AuthService } from 'src/app/services/auth.service';
import { NgForm } from '@angular/forms';
import { AlertService } from 'src/app/services/alert.service';
import {IdentityPage} from 'src/app/identity/identity.page';
import { Router } from '@angular/router';


@Component({
  selector: 'app-register',
  templateUrl: './register.page.html',
  styleUrls: ['./register.page.scss'],
})
export class RegisterPage implements OnInit {
  loading: boolean;// loading chamando
  country: any;
  password:any;
  private cod_country;

  constructor(
    private authService: AuthService,
    private navCtrl: NavController,
    private alertService: AlertService,
    public alertController: AlertController,
    private toast: ToastController,
    private router: Router,
 
  ) { 
     this.getCountry();
  }

  
  ngOnInit() {
  }

 //Get all country list
  getCountry(){
    this.authService.getCountry().subscribe(country=>{
      this.country = country;
    
    })
  } 

  // navigate to about page
  goToAboutPage(data) {
    this.router.navigate(['/identity'],{
      queryParams:data
    })
  }


}

【问题讨论】:

    标签: ionic-framework ionic5


    【解决方案1】:

    有多种方法可以将数据从一页传递到另一页。 方法一:

    使用Angular Router with params

    Page 1:

    goToNewPage(){
     this.router.navigate(['/identity'],{
       queryParams: JSON.Stringify(data)
     })
    }
    

    Page 2:

    import {ActivatedRoute} from '@angular/router';
    
    constructor(private activatedRoute: ActivatedRoute){
    this.activatedRoute.queryParams.subscribe(params => {
      console.log(params)
      });
    }
    

    方法二:

    创建一个GlobalDataProviderService。 运行命令:

    离子生成服务全局数据

    在 Globaldata 类中:

    public static userObject:any;

    在你的两个页面上导入这个类并初始化你的变量。

    Page 1:

    goToNewPage(){
        GlobaldataService.userObject = YourData;
        this.router.navigate(['/identity'])
    }
    

    Page 2:

    导入这个类:

    在 Constructor 或 lifeCycle Hook 中。

    yourVaribale = GlobaldataService.usrObject;
    

    实时更新方法三:

    查看我的其他答案:Real Time Data Update

    【讨论】:

    • yourVaribale = GlobaldataService.usrObject;我不明白。什么变量?我必须创建一个?像 const getData = GlobaldataService.usreObject; ??
    • 取决于您的要求。如果你想直接使用GlobaldataService,你可以使用它。
    • 在第一个例子中我需要导入一些东西吗?我是 IONIC 的新手,所以我还在学习。所以请我需要帮助。根据第一个答案,我有一个未定义的参数返回。所以我想知道我的 html 是否正确...
    • 在方法1中,您必须在接收数据的第二页上导入ActivatedRoute
    【解决方案2】:

    Page 1 html code

    <ion-row>
        <ion-col size="12">
          <ion-item>
            <ion-label position="stacked">E-Mail</ion-label>
            <ion-input type="email" [(ngModel)]="email"></ion-input>
          </ion-item>
          <ion-item>
            <ion-label position="stacked">Password</ion-label>
            <ion-input type="password" [(ngModel)]="password"></ion-input>
          </ion-item>
          <ion-item>
            <ion-label>Country</ion-label>
            <ion-select placeholder="Select Country" [(ngModel)]="country">
              <ion-select-option value="India">India</ion-select-option>
              <ion-select-option value="Usa">Usa</ion-select-option>
            </ion-select>
          </ion-item>
        </ion-col>
        <ion-col size="12" class="ion-text-center">
          <ion-button (click)="goToAboutPage()" expand="block" shape="round">
            Click me
          </ion-button>
        </ion-col>
      </ion-row>
    

    Page 1 ts code

    public country: string;
      public email: string;
      public password: string;   
    
    constructor(public nav: NavController) {}
    
    goToAboutPage() {
        let params: any = {
          country: this.country,
          email: this.email,
          password: this.password
        }
        this.nav.navigateForward('/identity', { state: params }); // params to pass object/array
      }
    

    Page 2

    constructor(public router: Router){
    if (router.getCurrentNavigation().extras.state) {
          const params = this.router.getCurrentNavigation().extras.state;
    console.log(params.email)
    console.log(params.password)
    console.log(params.country) 
        }
    }
    

    【讨论】:

    • 在你的例子中:this.nav.navigateForward('/identity', { state: params }) 请问我如何传递许多参数?我输入了我的示例 3 参数来传递:国家、电子邮件和密码。
    • @wonderCode 我更新我的答案请检查
    • 感谢您的回复。我一步一步地跟着你的代码,但我得到了 console.log undefine 。想知道我是否在我的 html 上犯了错误?你能告诉我为什么要取消定义吗?
    • 哦,请尝试打印“params”您在 params 中收到的内容
    • 还要检查这个网址“youtube.com/watch?v=C6LmKCSU8eM
    猜你喜欢
    • 1970-01-01
    • 2021-01-25
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多