register.page.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { Component, ViewChild, ElementRef } from '@angular/core';
  2. import { NavController } from '@ionic/angular';
  3. import { ToastController } from '@ionic/angular';
  4. @Component({
  5. selector: 'app-register',
  6. templateUrl: 'register.page.html',
  7. styleUrls: ['register.page.scss']
  8. })
  9. export class RegisterPage {
  10. username: string = "";
  11. password: string = "";
  12. confirmPassword: string = "";
  13. @ViewChild('usernameInput', { static: false })
  14. usernameInput!: ElementRef;
  15. @ViewChild('passwordInput', { static: false })
  16. passwordInput!: ElementRef;
  17. @ViewChild('confirmPasswordInput', { static: false })
  18. confirmPasswordInput!: ElementRef;
  19. constructor(private navCtrl: NavController, private toastController: ToastController) {}
  20. async register() {
  21. if (this.username === "" || this.password === "" || this.confirmPassword === "") {
  22. const toast = await this.toastController.create({
  23. message: '账号、密码和确认密码不能为空',
  24. duration: 2000,
  25. color: 'danger'
  26. });
  27. toast.present();
  28. if (this.username === "") {
  29. this.usernameInput.nativeElement.classList.add('error');
  30. }
  31. if (this.password === "") {
  32. this.passwordInput.nativeElement.classList.add('error');
  33. }
  34. if (this.confirmPassword === "") {
  35. this.confirmPasswordInput.nativeElement.classList.add('error');
  36. }
  37. } else {
  38. if (this.password === this.confirmPassword) {
  39. // 模拟注册成功,实际情况应该保存到数据库或其他存储介质中
  40. console.log('注册成功:', { username: this.username, password: this.password });
  41. this.navCtrl.navigateForward('/login');
  42. } else {
  43. const toast = await this.toastController.create({
  44. message: '密码不一致,请重新输入',
  45. duration: 2000,
  46. color: 'danger'
  47. });
  48. toast.present();
  49. }
  50. }
  51. }
  52. goToLogin() {
  53. this.navCtrl.navigateForward('/login');
  54. }
  55. }