edit-info.page.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. desc: '',
  13. };
  14. currentUser: Parse.User | undefined
  15. constructor(private navController: NavController) { }
  16. ngOnInit() {
  17. this.currentUser = Parse.User.current();
  18. if (this.currentUser) {
  19. // 修改uesrInfo赋值逻辑,仅加载被编辑的字段属性值
  20. let json = this.currentUser.toJSON();
  21. for (const key in json) {
  22. if (this.userInfo.hasOwnProperty(key)) {
  23. this.userInfo[key] = json[key]
  24. }
  25. }
  26. }
  27. console.log(this.userInfo)
  28. }
  29. save() {
  30. this.currentUser = Parse.User.current();
  31. if (this.currentUser) {
  32. console.log(this.userInfo)
  33. for (const key in this.userInfo) {
  34. if (this.userInfo.hasOwnProperty(key)) {
  35. this.currentUser.set(key, this.userInfo[key]);
  36. }
  37. }
  38. this.currentUser.save().then(() => {
  39. this.navController.back();
  40. }).catch((error) => {
  41. console.error('Error saving user data: ', error);
  42. });
  43. }
  44. }
  45. cancel() {
  46. this.navController.back();
  47. }
  48. }