Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 70612

Injecting service not working

$
0
0

@polska03 wrote:

I created something very basic and quick in a couple of minutes so it is easy to reproduce.

I created an app using:

ionic start blank --v2

then I create a provider:

ionic g provider FacebookFriends

I then put this code inside of on my provider:

import {Injectable, Inject} from 'angular2/core';
import {Http} from 'angular2/http';

/*
  Generated class for the FacebookFriends provider.

  See https://angular.io/docs/ts/latest/guide/dependency-injection.html
  for more info on providers and Angular 2 DI.
*/
@Injectable()
export class FacebookFriends {
  constructor(@Inject(Http) http) {
    this.http = http;
    this.data = null;
  }

  load() {
    if (this.data) {
      // already loaded data
      return Promise.resolve(this.data);
    }

    // don't have the data yet
    return new Promise(resolve => {
      // We're using Angular Http provider to request the data,
      // then on the response it'll map the JSON data to a parsed JS object.
      // Next we process the data and resolve the promise with the new data.
      this.http.get('path/to/data.json')
        .map(res => res.json())
        .subscribe(data => {
          // we've got back the raw data, now generate the core schedule data
          // and save the data for later reference
          this.data = data;
          resolve(this.data);
        });
    });
  }
}

I then try to inject this into app.js:

import {App, Platform} from 'ionic-angular';
import {TabsPage} from './pages/tabs/tabs';
import {FacebookFriends} from './providers/facebook-friends/facebook-friends';

@App({
  template: '<ion-nav [root]="rootPage"></ion-nav>',
  config: {}, // http://ionicframework.com/docs/v2/api/config/Config/,
  providers: [FacebookFriends]
})
export class MyApp {
  static get parameters() {
    return [[Platform]];
  }

  constructor(platform, private _facebookFriends) {
    this.rootPage = TabsPage;

    platform.ready().then(() => {
    });
      }
    }

This is all I did. When I run ionic serve I get many errors. I get that the there is an unknown token and it points at the @Inject and @Injectable words. I also get an unexpected token at the private _facebookFriends line.

Also if I try to add a types to the constructor so I would have platform:Platform and _facebookFriends:FacebookFriends I also get that the ':' are unknown tokens.

I am essentially just trying to call a service from my app.js, but it is not working.

Posts: 9

Participants: 3

Read full topic


Viewing all articles
Browse latest Browse all 70612

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>