代码之家  ›  专栏  ›  技术社区  ›  London804

TypeError.subscribe不是函数

  •  1
  • London804  · 技术社区  · 6 年前

    ERROR TypeError:this.entityService.getEntityType(…).subscribe不是函数

    //组成部分

    export class ProfileComponent implements OnInit {
    
    pageConfigs: any;
    facility: Facility;
    param1: any;
    entityConfig: any = {};
    snapshot: RouterStateSnapshot;
    
    constructor(
        private modelService: ModelService,
        private entityService: EntityService,
        private pageConfigsService: PageConfigsService,
        private route: ActivatedRoute,
        private router: Router
    ) {}
    
    ngOnInit() {
       this.snapshot = this.router.routerState.snapshot;
        console.log('snapshot', this.snapshot);
    
        console.log('ngOnInit snapshot url', this.snapshot.url);
    
        this.entityService.getEntityType(this.snapshot.url)
        .subscribe(
            response => {
                this.param1 = response
                console.log(this.param1);
            }
        );
        console.log('endpoint', this.entityConfig.endpoint);
    
        this.entityService.getEntities(this.entityConfig.endpoint)
        .subscribe(
            response => {
                this.facility = this.modelService.assignModelFromResponse(this.entityConfig.model, response[0]);
                console.log('facility', this.facility);
            }
        );
    
        this.pageConfigsService.getPageConfigs('provider_search', 'profile')
        .subscribe(
            response => {
                this.pageConfigs = response;
            }
        );
    }
    }
    

    //服务

    export class EntityService {
    entityConfig: any = {};
    
    constructor(
        private firebase: AngularFireDatabase,
        private http: HttpClient
    ) { }
    
    getEntities(entityType) {
        return this.http.get(`http://localhost:3000/${entityType}`);
        // return this.firebase.list('providers').valueChanges();
    }
    
    getEntityType(model) {
        switch (model) {
            case '/provider-search/profile/1?entity_type=provider':
            this.entityConfig = {
                name: 'providers',
                model: Provider,
                endpoint: 'providers'
            };
            break;
            case '/provider-search/profile/1?entity_type=facility':
            this.entityConfig = {
                name: 'facilities',
                model: Facility,
                endpoint: 'facilities'
            };
            break;
            case '/provider-search/profile/1?entity_type=pharmacy':
            this.entityConfig = {
                name: 'pharmacies',
                model: Pharmacy,
                endpoint: 'pharmacies'
            };
            break;
        }
    }
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Ian MacDonald    6 年前

    你的 getEntityType get 方法。为了订阅它,您需要将它包装成某种可观察的。

    不过,与其这样做,不如像使用同步函数一样使用它:

    getEntityType(...) {
       return { ... }
    }
    
    this.entityConfig = this.entityService.getEntityType(this.snapshot.url);
    console.log(this.entityConfig);