我在控制台中遇到错误(对于登录组件类)'没有字符串提供者!'但我没有在任何构造函数中使用任何String类型的参数。 我被困在这里过了两天,我无法弄清楚。
这是我的 main.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { LoginService } from "./service/login.service";
import { routing } from "./app.routes";
import { Ng2Webstorage, LocalStorage, LocalStorageService } from "ng2-webstorage";
import { CookieService, ANGULAR2_COOKIE_PROVIDERS } from "angular2-cookie/core";
import { MaterializeDirective } from "angular2-materialize";
import { HomeComponent } from "./components/home/home.component";
import { LoginComponent } from "./components/login/login.component";
import { SignUpComponent } from "./components/signup/signup.component";
import { HttpModule } from "@angular/http";
@NgModule({
imports: [
BrowserModule,
Ng2Webstorage,
routing,
HttpModule
],
declarations: [
AppComponent,
HomeComponent,
LoginComponent,
SignUpComponent
],
providers: [
LoginService,
ANGULAR2_COOKIE_PROVIDERS,
CookieService,
LocalStorageService
],
bootstrap: [AppComponent]
})
export class AppModule { }
这是我的 Login.component.ts
import { Component } from '@angular/core';
import 'rxjs/add/operator/map';
import { LoginService } from '../../service/login.service';
import { Router } from '@angular/router';
import { LocalStorageService } from 'ng2-webstorage';
import { CookieService } from 'angular2-cookie/core';
@Component({
selector: 'login',
templateUrl: 'login.component.html',
})
export class LoginComponent {
constructor(private loginService: LoginService,
private router: Router,
private localStorage: LocalStorageService,
private cookieService: CookieService) {
let token = this.cookieService.get('token');
if (token) {
this.loginService.setToken(token);
this.router.navigateByUrl('/text-items-list');
}
}
err: any = {
message: ""
};
onSubmit(form) {
var self = this;
self.err.message = "";
this.loginService.login({ email: form.controls.email.value, password: form.controls.password.value })
.subscribe(
function (response) {
if (response.status) {
if (form.controls.rememberMe.value) {
self.cookieService.put('token', response.token);
}
self.localStorage.clear();
self.localStorage.store("user", response.user);
self.loginService.setToken(response.token);
self.router.navigateByUrl('/text-items-list');
} else {
self.err.essage = response.message;
}
}
)
}
}
我正在注入我已创建的登录服务,其余参数来自依赖项。
这是我的 LoginService
import { Injectable } from "@angular/core";
import { Http, Headers } from "@angular/http";
import { Observable } from "rxjs/Rx";
import { LocalStorageService, SessionStorageService } from 'ng2-webstorage';
import { CookieService } from 'angular2-cookie/core';
@Injectable()
export class LoginService {
constructor(private http: Http, private localStorage: LocalStorageService, private cookieStorage: CookieService) { }
private getHeaders() {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
return headers;
}
setToken(encodedToken) {
this.localStorage.store("token", encodedToken);
}
getToken() {
return this.localStorage.retrieve("token");
}
login(credentials: { email: any, password: any }): Observable<any> {
const postBody = JSON.stringify(credentials);
return this.http.post('/login', postBody, {
headers: this.getHeaders()
}).map(res => res.json())
}
logout() {
this.cookieStorage.removeAll();
this.localStorage.clear('user');
}
singUp(credentials: { username: any, email: any, password: any }): Observable<any> {
const postBody = JSON.stringify(credentials);
return this.http.post('/login/signup', postBody, {
headers: this.getHeaders()
}).map(res => res.json())
}
checkUsernameAvailability(username: any) {
const postBody = { username: username };
let headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.post('/login/check-username-availability', postBody, {
headers: headers
}).map(res => res.json())
}
}
这是我在浏览器控制台中遇到的错误 error in browser's console