Rxjs 角度:可观测模型属性不存在

Rxjs 角度:可观测模型属性不存在,rxjs,observable,angular4,Rxjs,Observable,Angular4,从Angular 2升级到Angular 4后,我在尝试使用AOT编译时遇到以下错误: Property 'total' does not exist on type 'Observable<any>'. 然后将其与我的可观察对象一起使用,如下所示: public myData: Observable<any>; this.mySubscription = this.apiService.get('URL-HERE')

从Angular 2升级到Angular 4后,我在尝试使用AOT编译时遇到以下错误:

Property 'total' does not exist on type 'Observable<any>'.
然后将其与我的可观察对象一起使用,如下所示:

public myData: Observable<any>;
this.mySubscription = this.apiService.get('URL-HERE')
                        .subscribe(
                            response => this.myData = response,
                            error => this.feedbackService.displayError(<any>error),
                            () => this.feedbackService.stopLoading()
                        );
export class MyData {
    total: number;
    count: number;
    data: Array<any> = [];
}
... rest of component here
public myData: Observable<MyData>;
@Injectable()
export class MyService {
    getStuff(): Observable< MyType > {
        this.http.get('www.example.com/data').map(res => res.json());
    }
}
<div class="my-data">
    {{data$?.global?.statistics | async}}
</div>
data$: {
    global: {
        statistics: Observable< MyType >;
    }
}

所有值(包括
total
)都存在于返回的JSON中,我可以在HTML中毫无问题地使用它

证明AOT不喜欢Angular 4中的elvis操作符


我在所有组件上保持
可观察性
,并将所有包含
{{myData?.total}}
之类内容的条目包装在
*ngIf
检查(并删除?)中,错误消失了

证明AOT不喜欢Angular 4中的elvis操作符


我在所有组件上保持
可观察性
,并将所有包含
{{myData?.total}}
之类内容的条目包装在
*ngIf
检查(并删除?)中,错误消失了

您看到的编译器行为的变化,可能与aot的关系不大,而与更新的typescript的关系更大(如果您使用的是angular 2.4.9之前的版本,您必须使用typescript 2.0或更低版本,angular 4需要2.1或更高版本)


升级到typescript后,您可能还需要重新访问tsconfig设置,确保它们仍在执行您想要的操作。

您看到的编译器行为的变化,可能与aot关系不大,而与更新的typescript关系更大(如果您使用angular 2.4.9之前的版本,您必须使用typescript 2.0或更低版本,angular 4需要2.1或更高版本)


升级到typescript后,您可能还需要重新访问Tconfig设置,确保它们仍在执行您想要的操作。

让我们在此处添加一点上下文,我认为您的错误实际上发生在
组件.html
文件中,对吗

所以基本上,你有这样的东西:

public myData: Observable<any>;
this.mySubscription = this.apiService.get('URL-HERE')
                        .subscribe(
                            response => this.myData = response,
                            error => this.feedbackService.displayError(<any>error),
                            () => this.feedbackService.stopLoading()
                        );
export class MyData {
    total: number;
    count: number;
    data: Array<any> = [];
}
... rest of component here
public myData: Observable<MyData>;
@Injectable()
export class MyService {
    getStuff(): Observable< MyType > {
        this.http.get('www.example.com/data').map(res => res.json());
    }
}
<div class="my-data">
    {{data$?.global?.statistics | async}}
</div>
data$: {
    global: {
        statistics: Observable< MyType >;
    }
}
my component.component.ts

@Component({
    selector: 'app-my-component',
    templateUrl: './my-component.component.html',
    styleUrls: ['./my-component.component.scss']
})
export class MyComponent implements OnInit {
    data$: Observable< MyType >;

    construct(private myService: MyService) {
        this.data$ = myService.getStuff();
    }
}
最后,您的
my component.component.html
当前看起来是这样的:

public myData: Observable<any>;
this.mySubscription = this.apiService.get('URL-HERE')
                        .subscribe(
                            response => this.myData = response,
                            error => this.feedbackService.displayError(<any>error),
                            () => this.feedbackService.stopLoading()
                        );
export class MyData {
    total: number;
    count: number;
    data: Array<any> = [];
}
... rest of component here
public myData: Observable<MyData>;
@Injectable()
export class MyService {
    getStuff(): Observable< MyType > {
        this.http.get('www.example.com/data').map(res => res.json());
    }
}
<div class="my-data">
    {{data$?.global?.statistics | async}}
</div>
data$: {
    global: {
        statistics: Observable< MyType >;
    }
}
但实际上你想要的是:

data$: Observable< MyType >;

现在它知道
数据$
对象是
可观察的
,它后面的东西是返回对象的一部分,而不是
可观察的
类。

让我们在这里添加一点上下文,我想您的错误实际上发生在
组件.html
文件中,对吗

所以基本上,你有这样的东西:

public myData: Observable<any>;
this.mySubscription = this.apiService.get('URL-HERE')
                        .subscribe(
                            response => this.myData = response,
                            error => this.feedbackService.displayError(<any>error),
                            () => this.feedbackService.stopLoading()
                        );
export class MyData {
    total: number;
    count: number;
    data: Array<any> = [];
}
... rest of component here
public myData: Observable<MyData>;
@Injectable()
export class MyService {
    getStuff(): Observable< MyType > {
        this.http.get('www.example.com/data').map(res => res.json());
    }
}
<div class="my-data">
    {{data$?.global?.statistics | async}}
</div>
data$: {
    global: {
        statistics: Observable< MyType >;
    }
}
my component.component.ts

@Component({
    selector: 'app-my-component',
    templateUrl: './my-component.component.html',
    styleUrls: ['./my-component.component.scss']
})
export class MyComponent implements OnInit {
    data$: Observable< MyType >;

    construct(private myService: MyService) {
        this.data$ = myService.getStuff();
    }
}
最后,您的
my component.component.html
当前看起来是这样的:

public myData: Observable<any>;
this.mySubscription = this.apiService.get('URL-HERE')
                        .subscribe(
                            response => this.myData = response,
                            error => this.feedbackService.displayError(<any>error),
                            () => this.feedbackService.stopLoading()
                        );
export class MyData {
    total: number;
    count: number;
    data: Array<any> = [];
}
... rest of component here
public myData: Observable<MyData>;
@Injectable()
export class MyService {
    getStuff(): Observable< MyType > {
        this.http.get('www.example.com/data').map(res => res.json());
    }
}
<div class="my-data">
    {{data$?.global?.statistics | async}}
</div>
data$: {
    global: {
        statistics: Observable< MyType >;
    }
}
但实际上你想要的是:

data$: Observable< MyType >;

现在它知道
数据$
对象是
可观察的
,它后面的东西是返回对象的一部分,而不是
可观察的
类。

试试这个
公共myData:myData;
为什么
这个.myData=response
如果是可观察的?response是某种东西的流?er是哪一行ror reference to?使用我的服务调用中的示例进行了更新。由于我在整个应用程序的所有API调用中都使用了此设置,因此在编译时我会收到数百个此类错误。请尝试此
公共myData:myData;
为什么
this.myData=response
如果它是可观察的?响应是一个流?错误引用的是哪一行o?使用我的服务调用中的示例进行了更新。由于我在整个应用程序的所有API调用中都使用了此设置,因此在编译时,我会收到数百个此类错误。