【问题标题】:Uncaught (in promise): Error: StaticInjectorError(AppModule)[AuthServiceProvider -> HttpHeaders]:未捕获(承诺):错误:StaticInjectorError(AppModule)[AuthServiceProvider -> HttpHeaders]:
【发布时间】:2018-10-24 14:42:23
【问题描述】:

我正在开发一个关于 ionic v3 的项目。 我遇到了这个问题。 我的情况类似于#47492475 已经在 app.module.ts 中导入 HttpClientModule 但错误仍然存​​在。我把我的 .ts 文件留给你

auth-service.ts

import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { map } from 'rxjs/operators';

let apiUrl = "http://localhost/login_banana/api/";

/*
  Generated class for the AuthServiceProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class AuthServiceProvider {

  constructor(public http: HttpClient, public headers: HttpHeaders) {
    console.log('Hello AuthServiceProvider Provider');
  }

  postData(credentials, type){
    return new Promise((resolve, reject)=>{
        let headers = new HttpHeaders();
        this.http.post(apiUrl+type, JSON.stringify(credentials), {headers: headers}).subscribe(res =>{
            resolve(res);
        }, (err) =>{
            reject(err);
        });
    });
  }

}

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';

import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { SignupPage } from '../pages/signup/signup';
import { LoginPage } from '../pages/login/login';
import { WelcomePage } from '../pages/welcome/welcome';
import { AuthServiceProvider } from '../providers/auth-service/auth-service';
import { HttpClientModule } from '@angular/common/http';

@NgModule({
  declarations: [
    MyApp,
    HomePage,
    SignupPage,
    LoginPage,
    WelcomePage
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp),
    HttpClientModule
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage,
    SignupPage,
    LoginPage,
    WelcomePage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    AuthServiceProvider
  ]
})
export class AppModule {}

当我尝试访问这里时。出现错误

singup.ts

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { AuthServiceProvider } from '../../providers/auth-service/auth-service';
import { HomePage } from '../home/home';

/**
 * Generated class for the SignupPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-signup',
  templateUrl: 'signup.html',
})
export class SignupPage {

    responseData : any;
    userData = { "username":"", "password":"", "email":"", "name":"" };

  constructor(public navCtrl: NavController, public navParams: NavParams, public authService: AuthServiceProvider) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad SignupPage');
  }

  signup(){
    this.authService.postData(this.userData, "signup").then((result) =>{
        this.responseData = result;
        console.log(this.responseData);
        localStorage.setItem('userData', JSON.stringify(this.responseData))
        this.navCtrl.push(HomePage);
    }, (err) => {

    });
  }

}

这是错误

Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[AuthServiceProvider -> HttpHeaders]: 
  StaticInjectorError(Platform: core)[AuthServiceProvider -> HttpHeaders]: 
    NullInjectorError: No provider for HttpHeaders!
Error: StaticInjectorError(AppModule)[AuthServiceProvider -> HttpHeaders]: 
  StaticInjectorError(Platform: core)[AuthServiceProvider -> HttpHeaders]: 
    NullInjectorError: No provider for HttpHeaders!
    at _NullInjector.get (http://localhost:8100/build/vendor.js:1377:19)
    at resolveToken (http://localhost:8100/build/vendor.js:1675:24)
    at tryResolveToken (http://localhost:8100/build/vendor.js:1617:16)
    at StaticInjector.get (http://localhost:8100/build/vendor.js:1485:20)
    at resolveToken (http://localhost:8100/build/vendor.js:1675:24)
    at tryResolveToken (http://localhost:8100/build/vendor.js:1617:16)
    at StaticInjector.get (http://localhost:8100/build/vendor.js:1485:20)
    at resolveNgModuleDep (http://localhost:8100/build/vendor.js:11270:25)
    at _createClass (http://localhost:8100/build/vendor.js:11309:68)
    at _createProviderInstance$1 (http://localhost:8100/build/vendor.js:11281:26)
    at c (http://localhost:8100/build/polyfills.js:3:19752)
    at Object.reject (http://localhost:8100/build/polyfills.js:3:19174)
    at NavControllerBase._fireError (http://localhost:8100/build/vendor.js:51705:16)
    at NavControllerBase._failed (http://localhost:8100/build/vendor.js:51698:14)
    at http://localhost:8100/build/vendor.js:51745:59
    at t.invoke (http://localhost:8100/build/polyfills.js:3:14976)
    at Object.onInvoke (http://localhost:8100/build/vendor.js:5134:33)
    at t.invoke (http://localhost:8100/build/polyfills.js:3:14916)
    at r.run (http://localhost:8100/build/polyfills.js:3:10143)
    at http://localhost:8100/build/polyfills.js:3:20242

【问题讨论】:

    标签: angular ionic-framework ionic3


    【解决方案1】:

    您不应该在AuthServiceProviderconstructor 中注入HttpHeaders 作为服务。您可以简单地从'@angular/common/http' 导入和使用HttpHeadersHttpHeaders 不是 @Injectable() 服务或类似服务,它只是一个类。这就是错误NullInjectorError: No provider for HttpHeaders! 中的内容。

    import { HttpClient, HttpHeaders } from '@angular/common/http';
    import { Injectable } from '@angular/core';
    import { map } from 'rxjs/operators';
    
    let apiUrl = "http://localhost/login_banana/api/";
    
    /*
      Generated class for the AuthServiceProvider provider.
    
      See https://angular.io/guide/dependency-injection for more info on providers
      and Angular DI.
    */
    @Injectable()
    export class AuthServiceProvider {
    
      // HttpHeaders injection removed
      constructor(public http: HttpClient) {}
    
      postData(credentials, type){
        return new Promise((resolve, reject)=>{
            let headers = new HttpHeaders();
            this.http.post(apiUrl+type, JSON.stringify(credentials), {headers: headers}).subscribe(res =>{
                resolve(res);
            }, (err) =>{
                reject(err);
            });
        });
      }    
    }
    

    希望对您有所帮助!

    【讨论】:

    • 我的AuthServiceProvider 和你的一模一样。但仍然显示相同的错误:Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[AuthServiceProvider -> HttpClient]: StaticInjectorError(Platform: core)[AuthServiceProvider -> HttpClient]: NullInjectorError: No provider for HttpClient!
    猜你喜欢
    • 1970-01-01
    • 2018-09-21
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 2020-01-06
    • 2019-07-09
    • 2020-03-03
    相关资源
    最近更新 更多