new-post.page.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { Component, OnInit } from '@angular/core';
  2. import { ModalController } from '@ionic/angular';
  3. import { UserService } from '../services/user.service';
  4. @Component({
  5. selector: 'app-new-post',
  6. templateUrl: './new-post.page.html',
  7. styleUrls: ['./new-post.page.scss'],
  8. })
  9. export class NewPostPage implements OnInit {
  10. title: string = ''; // 声明 title 属性
  11. content: string = ''; // 声明 content 属性
  12. user: any;
  13. posts = [];
  14. constructor(private modalCtrl: ModalController, private userService: UserService) {}
  15. ngOnInit() {
  16. this.userService.getUserInfo().subscribe(userInfo => {
  17. if (userInfo) {
  18. this.user = userInfo;
  19. }
  20. });
  21. }
  22. async submitPost() {
  23. if (this.title.trim() === '' || this.content.trim() === '') {
  24. console.warn('Title and content cannot be empty');
  25. return;
  26. }
  27. // 打印日志以确认数据是否正确
  28. console.log('Submit Post:', { title: this.title, content: this.content });
  29. // 关闭模态窗口并返回数据
  30. await this.modalCtrl.dismiss({ title: this.title, content: this.content });
  31. }
  32. async cancel() {
  33. // 关闭模态窗口而不返回数据
  34. await this.modalCtrl.dismiss();
  35. }
  36. }