私は非常に基本的で迅速なものを数分で作成しましたので、再現は簡単です。注入サービスが動作しません
私が使用してアプリケーションを作成しました:
ionic start blank --v2
を、私はプロバイダを作成:
ionic g provider FacebookFriends
私はその後、私のプロバイダ上の内側にこのコードを配置:
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);
});
});
}
}
私はその後、これを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(() => {
});
}
}
これはすべて私がやったことです。私がionic serveを走らせると、私は多くのエラーを得る。私は未知のトークンがあることを知り、それは@Inject
と@Injectable
という単語を指しています。 private _facebookFriends
行に予期しないトークンがあります。
また、コンストラクタに型を追加しようとすると、platform:Platform
と_facebookFriends:FacebookFriends
があります。「:」は未知のトークンです。
私は基本的には私のapp.jsからサービスを呼び出そうとしていますが、動作していません。
アウトチェック[この回答](http://stackoverflow.com/a/35682570/3125880) – Abdulrahman
誰かthis - > this.data = null; ? –