我有以下服务:
import { Injectable } from '@angular/core';
import {Http} from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class MovieService {
static get parameters() {
return [[Http]];
}
constructor(private http:Http) {
}
searchMovies(movieName) {
var url = 'http://api.themoviedb.org/3/search/movie?query=&query=' + encodeURI(movieName) + '&api_key=5fbddf6b517048e25bc3ac1bbeafb919';
var response = this.http.get(url).map(res => res.json());
return response;
}
}
(我实际上并不确定获取参数功能的用途是什么,因为我从一个例子中得到它,但它的存在似乎并没有引起任何问题)
我正在尝试将服务注入组件:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {MovieService} from '../../services/psa-web-api.service';
@Component({
templateUrl: 'build/pages/browsed/browsed.html',
providers:[MovieService]
})
export class BrowsedPage {
constructor(private movieService: MovieService, public navCtrl: NavController) {
}
}
但每次我提供应用程序并通过浏览器访问它时,都会发出以下控制台错误:
EXCEPTION: Error: Uncaught (in promise):
EXCEPTION: Error in ./MyApp class MyApp_Host - inline template:0:0
ORIGINAL EXCEPTION: No provider for ConnectionBackend! ORIGINAL STACKTRACE: Error: DI Exception
我没有能够在网上发现任何有关此错误的内容,但这篇文章并非超级有用:https://groups.google.com/forum/#!topic/angular/z7jnb6goZUQ
编辑:添加此内容以获取更多上下文,我的app.ts文件:
import { Component} from '@angular/core';
import { Platform, ionicBootstrap } from 'ionic-angular';
import {HTTP_PROVIDERS } from '@angular/http';
@Component({
template: '<ion-nav [root]="rootPage"></ion-nav>',
providers: []
})
export class MyApp {...}
ionicBootstrap(MyApp, [HTTP_PROVIDERS]);
答案 0 :(得分:1)
我不知道 Ionic 所以我可能错了,但是在Angular2 HttpModule 是处理 Http 所必需的。 ( RC5更高版本 )
import { HttpModule} from '@angular/http';
@NgModule({
imports: [
BrowserModule,
HttpModule, //<------HttpModule is required to import
],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
<强> RC4 强>
import {HTTP_PROVIDERS} from '@angular/http';
bootstrap(AppComponent, [
HTTP_PROVIDERS, //<-----need to inject here
....
])
答案 1 :(得分:1)
我的app.ts
看起来像这样
import {HTTP_PROVIDERS} from '@angular/http';
@Component({
templateUrl: "build/app.html",
providers: [HTTP_PROVIDERS]
})
然后
ionicBootstrap(MyApp) //without the HTTP_PROVIDERS
答案 2 :(得分:0)
尝试不使用get parameters
方法,因为不再需要它(它在以前版本的Ionic2中使用过,我认为,所以可能你得到的例子没有更新)
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class MovieService {
constructor(private http:Http) {
}
searchMovies(movieName) {
var url = 'http://api.themoviedb.org/3/search/movie?query=&query=' + encodeURI(movieName) + '&api_key=5fbddf6b517048e25bc3ac1bbeafb919';
var response = this.http.get(url).map(res => res.json());
return response;
}
}
也可以尝试不导入HTTP_PROVIDERS
import { HTTP_PROVIDERS } from '@angular/http';
所以你的ionicBootstrap
看起来像这样:
ionicBootstrap(MyApp);