story.service.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // 假设文件路径为: d:\workspace\20222670105\novel-app\src\app\services\story.service.ts
  2. import { Injectable } from '@angular/core';
  3. import { BehaviorSubject } from 'rxjs';
  4. @Injectable({
  5. providedIn: 'root'
  6. })
  7. export class StoryService {
  8. private storiesSource = new BehaviorSubject<any[]>([]);
  9. public stories$ = this.storiesSource.asObservable();
  10. getStories(): any[] {
  11. return this.storiesSource.getValue();
  12. }
  13. setStories(stories: any[]): void {
  14. this.storiesSource.next(stories);
  15. }
  16. generateUniqueId(): string {
  17. return Math.random().toString(36).substr(2, 9);
  18. }
  19. addStory(story: any): Promise<void> {
  20. return new Promise((resolve, reject) => {
  21. setTimeout(() => {
  22. const currentStories = this.getStories();
  23. currentStories.push(story);
  24. this.setStories(currentStories);
  25. console.log('故事已添加:', story);
  26. resolve();
  27. }, 1000);
  28. });
  29. }
  30. }