123456789101112131415161718192021222324252627282930313233343536 |
- // 假设文件路径为: d:\workspace\20222670105\novel-app\src\app\services\story.service.ts
- import { Injectable } from '@angular/core';
- import { BehaviorSubject } from 'rxjs';
- @Injectable({
- providedIn: 'root'
- })
- export class StoryService {
- private storiesSource = new BehaviorSubject<any[]>([]);
- public stories$ = this.storiesSource.asObservable();
- getStories(): any[] {
- return this.storiesSource.getValue();
- }
- setStories(stories: any[]): void {
- this.storiesSource.next(stories);
- }
- generateUniqueId(): string {
- return Math.random().toString(36).substr(2, 9);
- }
- addStory(story: any): Promise<void> {
- return new Promise((resolve, reject) => {
- setTimeout(() => {
- const currentStories = this.getStories();
- currentStories.push(story);
- this.setStories(currentStories);
- console.log('故事已添加:', story);
- resolve();
- }, 1000);
- });
- }
- }
|