未处理的承诺拒绝:没有AuthHttp的提供者! ;区域:角度为2-jwt

时间:2017-02-06 09:53:03

标签: javascript angular angular2-jwt

dashboard.service.ts

import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable }     from 'rxjs/Observable';
import { AuthHttp } from 'angular2-jwt';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

constructor (private http: Http,private authHttp: AuthHttp) {}

当我这样做时,会出现错误Error: Uncaught (in promise): Error: DI

app.module.ts

import { HttpModule } from '@angular/http';
import { AuthModule } from 'angular2-jwt';
.
imports: [
  HttpModule,
  AuthModule   
],

我如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

试试这个

import { AuthModule, AuthConfigConsts} from 'angular2-jwt';

const authConfig: IAuthConfig = {
    headerName: AuthConfigConsts.DEFAULT_HEADER_NAME, // change to your header name
    headerPrefix: AuthConfigConsts.HEADER_PREFIX_BEARER, // or null or something else
    tokenName: AuthConfigConsts.DEFAULT_TOKEN_NAME, // change to your token name
    tokenGetter: () => localStorage.getItem(authConfig.tokenName) as string,
    noJwtError: false,
    noClientCheck: false,
    globalHeaders: [],
    noTokenScheme: false
};

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        HttpModule,
        AuthModule.forRoot(authConfig)
    ],
})

export class AppModule { }

选项2

String[] grades ={"A","B+","B","C+","C", "D+", "D", "E"};

String[] credits ={"1","2","3","4"};
double [] points1 = {4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0 };


Spinner spinner;
TextView TextView1;
TextView TextView2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView1 = (TextView) findViewById(R.id.gradePoints);
    TextView2 = (TextView) findViewById(R.id.gradeValue);

    spinner = (Spinner) findViewById(R.id.spinner);


    ArrayAdapter<String> adapter1 =
            new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, grades);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter1);
    spinner.setOnItemSelectedListener(onItemSelectedListener1);

    Spinner spin2 = (Spinner) findViewById(R.id.spinner2);
    ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, credits);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin2.setAdapter(adapter2);
    spin2.setOnItemSelectedListener(this);
}

AdapterView.OnItemSelectedListener onItemSelectedListener1 = new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String s1 = String.valueOf(points1[position]);
        TextView1.setText(s1);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
};


public void onItemSelected(AdapterView<?> parent, View v, int position, long id ){
    Spinner spin = (Spinner)parent;
    Spinner spin2 = (Spinner)parent;

    if(spin.getId() == R.id.spinner)
    {
        Toast.makeText(this, "Your choice :" + grades[position],Toast.LENGTH_SHORT).show();
    }
    if(spin2.getId() == R.id.spinner2)
    {
        Toast.makeText(this, "Your choice :" + credits[position],Toast.LENGTH_SHORT).show();
    }

}

public void onNothingSelected(AdapterView<?> parent){
    Toast.makeText(this, "Choose grades :", Toast.LENGTH_SHORT).show();
}