IONIC 2 - 没有标题提供者

时间:2017-01-25 11:12:45

标签: angular ionic2

我使用ionic2 rc.5

这是我的提供者:

import {Http, Headers, RequestOptions} from '@angular/http';
import { Injectable, Component } from '@angular/core';
import { NavController, AlertController, LoadingController, Loading } from 'ionic-angular';
import {Observable} from 'rxjs/Observable';
import {Geolocation} from 'ionic-native';
import 'rxjs/add/operator/map';

@Injectable()
export class Setup {

    loading: Loading;

    constructor(public http: Http, public headers: Headers, private alertCtrl: AlertController, private loadingCtrl: LoadingController) {}



    public generalSetupCheck () {
        if ( localStorage.getItem('guards') == null ) {
            console.log("CREATE GUARDS localStorage")
            localStorage.setItem('guards', JSON.stringify([]))
        }
        if ( localStorage.getItem('sms_list') == null ) {
            console.log("CREATE SMS_LIST localStorage")
            localStorage.setItem('sms_list', JSON.stringify([]))
        }
    }
    public getProfile (token) {
        this.showLoading()
        console.log("GET USER PROFILE")
            this.headers.append('Authorization','Token ' + token);
            this.http.get('http://30d2ef2d.ngrok.io/profile/', { headers: this.headers }).subscribe(data => {
                console.log("USER PROFILE: success")
                localStorage.setItem('user', JSON.stringify(data.json()));
                this.getGPS()

            }, error => {
                console.log("error")
            }, () => {});
    }
    public getGPS () {
        console.log("GET GPS LOCATION")
        Geolocation.getCurrentPosition().then(pos => {
            var coords = pos.coords.longitude +","+ pos.coords.latitude;
            console.log('FOUND GPS LOCATION: lat: ' + pos.coords.latitude + ', lon: ' + pos.coords.longitude)
            this.determinePA(coords)
        });
    }
    public determinePA (coords) {
        console.log("FINDING PROTECTED AREA")
        this.http.get('http://30d2ef2d.ngrok.io/protected-area/list/' + coords + '/', { headers: this.headers }).subscribe(data => {
                if ( data.json().length > 0 ) {
                    console.log("FOUND PROTECTED AREA")
                    console.log(data.json().length)
                } else {
                    console.log("PROTECTED AREA NOT FOUND")
                }           
            }, error => {
                console.log("error")
            }, () => {});
    }

    showLoading() {
    this.loading = this.loadingCtrl.create({
      content: 'Please wait...'
    });
    this.loading.present();
  }
  showError(text) {
    setTimeout(() => {
      this.loading.dismiss();
    });

    let alert = this.alertCtrl.create({
      title: 'Authentication problem',
      subTitle: text,
      buttons: ['OK']
    });
    alert.present(prompt);
  }
}

但是它给了我错误没有提供者标题:

error_handler.js:47EXCEPTION: Error in ./MyApp class MyApp - inline template:0:0 caused by: No provider for Headers!
ErrorHandler.handleError @ error_handler.js:47
(anonymous) @ application_ref.js:210
t.invoke @ polyfills.js:3
onInvoke @ ng_zone.js:236
t.invoke @ polyfills.js:3
e.run @ polyfills.js:3
(anonymous) @ polyfills.js:3
t.invokeTask @ polyfills.js:3
onInvokeTask @ ng_zone.js:227
t.invokeTask @ polyfills.js:3
e.runTask @ polyfills.js:3
i @ polyfills.js:3
error_handler.js:49ORIGINAL EXCEPTION: No provider for Headers!
ErrorHandler.handleError @ error_handler.js:49
(anonymous) @ application_ref.js:210
t.invoke @ polyfills.js:3
onInvoke @ ng_zone.js:236
t.invoke @ polyfills.js:3
e.run @ polyfills.js:3
(anonymous) @ polyfills.js:3
t.invokeTask @ polyfills.js:3
onInvokeTask @ ng_zone.js:227
t.invokeTask @ polyfills.js:3
e.runTask @ polyfills.js:3
i @ polyfills.js:3
error_handler.js:52ORIGINAL STACKTRACE:
ErrorHandler.handleError @ error_handler.js:52
(anonymous) @ application_ref.js:210
t.invoke @ polyfills.js:3
onInvoke @ ng_zone.js:236
t.invoke @ polyfills.js:3
e.run @ polyfills.js:3
(anonymous) @ polyfills.js:3
t.invokeTask @ polyfills.js:3
onInvokeTask @ ng_zone.js:227
t.invokeTask @ polyfills.js:3
e.runTask @ polyfills.js:3
i @ polyfills.js:3
error_handler.js:53Error: No provider for Headers!
    at NoProviderError.BaseError [as constructor] (errors.js:24)
    at NoProviderError.AbstractProviderError [as constructor] (reflective_errors.js:41)
    at new NoProviderError (reflective_errors.js:72)
    at ReflectiveInjector_._throwOrNull (reflective_injector.js:758)
    at ReflectiveInjector_._getByKeyDefault (reflective_injector.js:786)
    at ReflectiveInjector_._getByKey (reflective_injector.js:749)
    at ReflectiveInjector_.get (reflective_injector.js:558)
    at AppModuleInjector.get (module.ngfactory.js:270)
    at AppModuleInjector.getInternal (module.ngfactory.js:379)
    at AppModuleInjector.NgModuleInjector.get (ng_module_factory.js:94)

1 个答案:

答案 0 :(得分:5)

Headers不是提供者,因此无法注入。您应该将其初始化为new Headers()

let headers: Headers = new Headers();
headers.append('Authorization','Token ' + token);

但是如果你想在整个班级中使用它们,请在班级中定义它们:

private headers: Headers = new Headers();

constructor(...) {}

然后,您可以将它们用作this.headers