【问题标题】:pass params from one page to another in angular ionic以角度离子将参数从一页传递到另一页
【发布时间】:2019-09-25 18:36:39
【问题描述】:

我想将参数从注册页面传递到 signupotp 页面。在我的 signupotp 页面中,由于参数(电子邮件和移动设备)未传递到 signupotp 页面,因此无法识别 otp。我的 backedn 编码为 otp 验证参数(移动、电子邮件、otp)应该被验证。所以它不能正常工作。我试图在注册页面中初始化我的数据并传递给 signupotp 页面。但没有工作。如何做到这一点?

SIGNUP.PAGE.TS

import { Component, OnInit } from '@angular/core';
import {ApiService} from '../api.service';
import{Router} from '@angular/router';
import { FormsModule } from '@angular/forms';
import {SignupotpPage} from '../signupotp/signupotp.page';

@Component({
selector: 'app-signup',
templateUrl: './signup.page.html',
styleUrls: ['./signup.page.scss'],
})
export class SignupPage implements OnInit {

Mobile:any;
Email:any;
data:any;
otp:any;
checked:any;


constructor(private api:ApiService,private router:Router,public signupotp:SignupotpPage) {}

ngOnInit() {
}

Continue(Mobile,Email){
const user= {
Mobile:Mobile,
  Email:Email
}
console.log(Mobile);
console.log(Email);

this.api.SignupUser(user).subscribe(data=>{
console.log(data);
this.signupotp.initialisedata(Mobile,Email);
this.router.navigate(['signupotp']);

});
}
}

SIGNUPOTP.PAGE.TS

import { Component, OnInit } from '@angular/core';
import {ApiService} from '../api.service';
import{Router} from '@angular/router';
@Component({
selector: 'app-signupotp',
templateUrl: './signupotp.page.html',
styleUrls: ['./signupotp.page.scss'],
})
export class SignupotpPage implements OnInit {

Mobile:any;
Email:any;
otp:any;
data:any;

constructor(private api:ApiService,private router:Router) {


}
initialisedata(email,mobile)
{
console.log("HUUUU");
console.log(email,mobile);
this.Email = email;
this.Mobile = mobile; 
}
ngOnInit() {
}



Confirm(otp){
const user= {
otp:otp,
mobile:this.Mobile,
email:this.Email
}
console.log(otp); 
console.log("hey how are you")


this.api.Verifysignupotp(user).subscribe(data=>{
console.log(data);
this.data =  data;
if(this.data.Msg)
{
alert(this.data.Msg);
}
else
{
this.router.navigate(['/']);
}

});
}
}

服务.ts

SignupUser(user):Observable<Object>{
console.log(user);

console.log('getting the signup data from user');
return this.http.post<Object>(`${this.base_url}/signup`,user);

}

 Verifysignupotp(user): Observable<Object>{
    console.log(user);
    console.log('getting all Listing from the server');
    return this.http.post<Object>(`${this.base_url}/verifysignupotp`,user);
  }

【问题讨论】:

标签: angular typescript ionic-framework ionic4


【解决方案1】:

在 Angular 中有多种方法可以通过路由的 url 传递参数,如下所示:this.router.navigate[‚path‘, param] 在您的 app-routing.module.ts 中,您必须编写如下内容:

export const routes: Routes = [
  { path: 'path/:param', component: yourComponent }
];

或者你只是在你的注册服务中的一些变量上写参数,然后从你想使用它们的任何地方读取它们。

在您的情况下,我将在您的 service.ts 中创建一个名为 user 的对象。 当您在注册组件/页面中获取数据时,您将其写入 service.ts 的 user 对象 导航到您的 singupotp 页面/组件后,您可以从服务中读取数据,并可能使用其他数据扩展对象。

【讨论】:

  • 也许重要的是要知道,当您通过路由传递参数时,所有数据都可以在 url 中以纯文本形式看到
  • 没有用于 ionic .Page 的组件在 ionic 中使用。如何在离子中做到这一点
  • 你仍然在 Ionic 中使用组件,即使它们被命名为页面。无论如何,路由上的导航工作相同,服务的策略不取决于它是组件还是不同的东西
  • 我目前正在开发一个 Ionic 项目,我更喜欢将服务与 rxjs Observables 结合使用来实现组件之间的通信
  • 我想通过电子邮件和手机到 OTP 页面进行验证。但它显示错误
猜你喜欢
  • 1970-01-01
  • 2016-12-19
  • 2019-08-19
  • 2020-01-30
  • 1970-01-01
  • 2018-12-13
  • 1970-01-01
  • 1970-01-01
  • 2018-04-24
相关资源
最近更新 更多