Search code examples
angularangular2-directivesangular2-servicesangular2-components

Angular RC 1 - Find out when a subscribe succeeds


Im running into some issues since migrating to RC1 one of which is within one of my components, I have a service which grabs some json from a laravel api and subscribes to it then within the component I have the following

        import {Component, ElementRef, OnInit} from '@angular/core';
        import {ProjectsMainApi} from "../../../services/projects-main";

        declare var jQuery: any;
        declare var subscribe: any;

        import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from '@angular/router-deprecated';

        @Component({
            selector: 'projects',
            templateUrl: './app/components/Projects/list/index.html',
            directives: [ROUTER_DIRECTIVES]
        })

        export class ProjectsListComponent implements OnInit {

            elementRef: ElementRef;

            project: Object;

            constructor(elementRef: ElementRef, private _projectsmainapi: ProjectsMainApi) {
                this.elementRef = elementRef;
                this.project = this._projectsmainapi.project$;

                this._projectsmainapi.getProjectsMain();        
            }
            ngOnInit() {


                this.project.subscribe(() => {

                   alert('subscribed');

                });
            }
        }

the part that is failing it the lines

                this.project.subscribe(() => {

                   alert('subscribed');

                });

before upgrading the compiler compiled this no problem and it worked but since upgrading I am getting this error error TS2339: Property 'subscribe' does not exist on type 'Object'.

My service file hasnt changed at all and is laid out as follows

    import {Http, Headers, Response} from "@angular/http"
    import {Injectable} from "@angular/core"
    import {IProjectsMain} from "../interfaces/IProjectsMain"
    import 'rxjs/Rx';
    import {Observable} from 'rxjs/Observable';
    import {Observer} from 'rxjs/Observer';
    import 'rxjs/add/operator/share';
    import 'rxjs/add/operator/map';

    @Injectable()
    export class ProjectsMainApi {

        apiUrl: string = "http://www.test.io/api/projects";

        headers: Headers = new Headers;

        project$: Observable<IProjectsMain[]>;

        private _ProjectsMainObserver: Observer<IProjectsMain[]>;

        private _dataStore: {

            project: IProjectsMain[]

        };

        constructor(private _http: Http) {

            this.headers.append('Content-Type', 'application/x-www-form-urlencoded');

            this.headers.append('X-Requested-With', 'XMLHttpRequest');

            //this.project$ = new Observable(observer => this._ProjectsMainObserver = observer).share();
            this.project$ = new Observable<IProjectsMain[]>(observer => this._ProjectsMainObserver = observer).share();
            this._dataStore = { project: [] };
        }

        public getProjectsMain() {

            this._http.get(this.apiUrl).map(response => response.json()).subscribe(data => {

                this._dataStore.project = data.project;

                this._ProjectsMainObserver.next(this._dataStore.project);

            }, error => console.log('Could not load projects.'),
                () => "doneskies");

        }

    }

Having spent the day Googling and messing around with the code Im at a loss as to how I can fix this and make it compile so now Im hoping that someone may know how to tackle it another way i.e I need to be able to tell when a subscription happens and it succeeds but from within my component, the end result should trigger the alert thats in my component file


Solution

  • Change the type of the variable project to Observable<any> or Observable<IProjectsMain[]> as in your service.

    export class ProjectsListComponent implements OnInit {
        elementRef: ElementRef;
        project: Observable<any>;
    ...
    }
    

    It was working before because you probably changed your tsconfig.json to have?:

    "noEmitOnError": true