gpt4 book ai didi

angular - IONIC 2 - 没有 header 提供者

转载 作者:行者123 更新时间:2023-12-02 16:55:36 24 4
gpt4 key购买 nike

我正在使用 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)

最佳答案

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

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

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

private headers: Headers = new Headers();

constructor(...) {}

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

关于angular - IONIC 2 - 没有 header 提供者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41850110/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com