edit-info.page.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. for (const key in this.userInfo) {
  36. if (this.userInfo.hasOwnProperty(key)) {
  37. this.currentUser.set(key, this.userInfo[key]);
  38. }
  39. }
  40. this.currentUser.save().then(() => {
  41. this.navController.back();
  42. }).catch((error) => {
  43. console.error('Error saving user data: ', error);
  44. });
  45. }
  46. }
  47. cancel() {
  48. this.navController.back();
  49. }
  50. }