edit-info.page.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { Component, OnInit } from '@angular/core';
  2. import { NavController } from '@ionic/angular';
  3. import * as Parse from 'parse';
  4. @Component({
  5. selector: 'app-edit-info',
  6. templateUrl: './edit-info.page.html',
  7. styleUrls: ['./edit-info.page.scss'],
  8. })
  9. export class EditInfoPage implements OnInit {
  10. userInfo: any = {
  11. name: '',
  12. mobile: '',
  13. gender: '',
  14. birthday: ''
  15. };
  16. currentUser:Parse.User|undefined
  17. constructor(private navController: NavController) {}
  18. ngOnInit() {
  19. this.currentUser = Parse.User.current();
  20. if (this.currentUser) {
  21. // 修改uesrInfo赋值逻辑,仅加载被编辑的字段属性值
  22. let json = this.currentUser.toJSON();
  23. for (const key in json) {
  24. if (this.userInfo.hasOwnProperty(key)) {
  25. this.userInfo[key] = json[key]
  26. }
  27. }
  28. }
  29. console.log(this.userInfo)
  30. }
  31. save() {
  32. this.currentUser = Parse.User.current();
  33. if (this.currentUser) {
  34. console.log(this.userInfo)
  35. this.userInfo.birthday = this.userInfo.birthday || new Date()
  36. this.userInfo.birthday = new Date(this.userInfo.birthday);
  37. for (const key in this.userInfo) {
  38. if (this.userInfo.hasOwnProperty(key)) {
  39. this.currentUser.set(key, this.userInfo[key]);
  40. }
  41. }
  42. this.currentUser.save().then(() => {
  43. this.navController.back();
  44. }).catch((error) => {
  45. console.error('Error saving user data: ', error);
  46. });
  47. }
  48. }
  49. cancel() {
  50. this.navController.back();
  51. }
  52. }