gpt4 book ai didi

angular - 类型 'Observable' 不可分配给类型 'Observable'

转载 作者:行者123 更新时间:2023-12-04 22:42:10 26 4
gpt4 key购买 nike

我正在使用 Angular 8 应用程序。我正在使用 Observable 模式形式 rxjs。但是我仍然在 Observable 行上遇到错误。

当然是用谷歌搜索错误。但是没有找到正确的answare。

所以我导入了这个:

import { Subject, Observer, Observable } from 'rxjs';

我有这一行:

    public lessonList$: Observable<Lesson[]> = this.lessonListSubject.asObservable();

但我仍然得到这个错误:


Type 'Observable<unknown>' is not assignable to type 'Observable<Lesson[]>'.
Type '{}' is missing the following properties from type 'Lesson[]': length, pop, push, concat, and 26 more.ts(2322)

错误会消失。

谢谢

好的,这是界面类(class):

export interface Lesson {
id: number;
description: string;
duration?: string;
completed?: boolean;

}

这是整个文件:

import * as _ from 'lodash';
import { Lesson } from '../shared/model/lesson';
import { Subject, Observer, Observable } from 'rxjs';


class DataStore {

private lessons: Lesson[] = [];

private lessonListSubject = new Subject();

public lessonList$: Observable<Lesson[]> = this.lessonListSubject.asObservable();



initaliseLessonList(newList: Lesson[]) {
this.lessons = _.clone(newList);
this.broadCast();


}
addLesson(newLesson: Lesson) {

this.lessons.push(newLesson);
this.broadCast();
}

deleteLesson(deleted: Lesson) {
_.remove(this.lessons, lesson => lesson.id === deleted.id);
this.broadCast();


}

toggleView(toggled: Lesson) {
// tslint:disable-next-line: no-shadowed-variable
const lesson = _.find(this.lessons, lesson => lesson.id === toggled.id);

lesson.completed = !lesson.completed;
this.broadCast();


}

broadCast() {
this.lessonListSubject.next(_.cloneDeep(this.lessons));

}
}

export const store = new DataStore();









最佳答案

替换

private lessonListSubject = new Subject();

private lessonListSubject = new Subject<Lesson[]>();

这样,你就有了一个Subject<Lesson[]>而不是 Subject<unknown> .

关于angular - 类型 'Observable<unknown>' 不可分配给类型 'Observable<Lesson[]>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57840507/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com