键入'Observable <{} | “ GeneralSettings>”不能分配给“ Observable <generalsettings>”类型

时间:2018-10-30 06:12:22

标签: angular

我收到此错误:

Type 'Observable<{} | GeneralSettings>' is not assignable to type 'GeneralSettings | Observable<GeneralSettings> | Promise<GeneralSettings>'.
Type 'Observable<{} | GeneralSettings>' is not assignable to type 'Observable<GeneralSettings>'.
Type '{} | GeneralSettings' is not assignable to type 'GeneralSettings'.
Type '{}' is not assignable to type 'GeneralSettings'.
Property 'address' is missing in type '{}'.
Expected 0 type arguments, but got 1.

这是我的代码:

 import { ActivatedRouteSnapshot, Resolve, RouterStateSnapshot } from "@angular/router";
 import { Injectable } from "@angular/core";
 import { Observable } from "rxjs/Observable";
 import { GeneralSettings } from "../../../../models/generalSettings";
 import { GeneralSettingsService } from "../../../../_services/apis/general-settings.service";

 @Injectable()
 export class GeneralSettingsResolver implements Resolve<GeneralSettings> {

 constructor(private api: GeneralSettingsService) {
 }


 resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<GeneralSettings> | Promise<GeneralSettings> | GeneralSettings {

    return this.api.get("/") //fineOne
        .catch(() => {
            return Observable.empty<GeneralSettings>()
        });
 } 
 }

这有什么问题? 我是Angular的新手,我只是尝试运行此项目,但没有运行!

1 个答案:

答案 0 :(得分:1)

如果您使用的是rxjs <6版本,则可以使用empty()函数,如下所示:

 import { ActivatedRouteSnapshot, Resolve, RouterStateSnapshot } from "@angular/router";
 import { Injectable } from "@angular/core";
 import { Observable } from "rxjs/Observable";
 import { GeneralSettings } from "../../../../models/generalSettings";
 import { GeneralSettingsService } from "../../../../_services/apis/general-settings.service";

 @Injectable()
 export class GeneralSettingsResolver implements Resolve<GeneralSettings> {

 constructor(private api: GeneralSettingsService) {
 }


 resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<{} | GeneralSettings> | Promise<GeneralSettings> | GeneralSettings  {

    return this.api.get("/") //fineOne
        .catch(() => {
            return Observable.empty()
        });
    } 
 }