未捕获的错误:无法解析LoginService的所有参数:(?,?)

时间:2018-07-26 11:06:24

标签: angular typescript injectable

我是Angular 6的新手。

我在github上看到了很多错误报告,但是没有一个可以回答我的问题。

当我尝试提供从http接收信息的服务时,出现syntax error下的错误。

Uncaught Error: Can't resolve all parameters for LoginService: (?, ?).

清单:

  • 我尝试从登录服务和应用中删除所有注入 作品。
  • 我主要跟随Hero Tour教程学习一些自己的代码
  • 我在app.module.ts中添加了提供程序
  • 我在所有可注射服务中添加了@Injectable

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import {FormsModule} from "@angular/forms";
import {HttpClientModule} from "@angular/common/http";
import { LoginboxComponent } from './loginbox/loginbox.component';
import {LoginService} from "./login.service";
import {CookiesService} from "./cookies.service";

@NgModule({
  declarations: [
    AppComponent,
    LoginboxComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpClientModule
  ],
  providers: [
    LoginService,
    CookiesService,
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

loginbox.components.ts

import { Component, OnInit } from '@angular/core';
import {LoginService} from "../login.service";

@Component({
  selector: 'app-loginbox',
  templateUrl: './loginbox.component.html',
  styleUrls: ['./loginbox.component.css']
})
export class LoginboxComponent implements OnInit {

  constructor(private loginService: LoginService) { }

  ngOnInit() {
  }

  loggedin:boolean;

  add(email: string,password:string): void {
    email = email.trim();
    password = password.trim();
    if (!name) { return; }
    this.loginService.auth(email,password)
      .subscribe(user => {
        if(user.id>0)this.loggedin=true;
      });
  }

}

login.service.ts

import { Injectable } from '@angular/core';
import { User } from './objects/user';
import { Observable, of } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import {USERMGMT} from "./constants/serverconfig";
import {CookiesService} from "./cookies.service";
import {catchError, tap} from "rxjs/operators";

@Injectable({
  providedIn: 'root'
})

class AuthData{
  email:string;
  password:string;
}

export class LoginService {

  private heroesUrl = USERMGMT+'api/public/login/getUserObject.php';  // URL to web api

  constructor(private http: HttpClient, private cookies: CookiesService) { }
  authData:AuthData;

  auth(email:string,password:string): Observable<User>{
    const httpOptions = {
      headers: new HttpHeaders({ 'Content-Type': 'application/json' })
    };
    this.authData.email=email;
    this.authData.password=password;

    return this.http.post<AuthData>(this.heroesUrl, this.authData, httpOptions).pipe(
      tap((user: User) => {
        this.cookies.setCookie("usr",JSON.stringify(user),1);
      }),
      catchError(this.handleError<User>('fetchuser'))
    );


  }
  /**
   * Handle Http operation that failed.
   * Let the app continue.
   * @param operation - name of the operation that failed
   * @param result - optional value to return as the observable result
   */
  private handleError<T> (operation = 'operation', result?: T) {
    return (error: any): Observable<T> => {

      // TODO: send the error to remote logging infrastructure
      console.error(error); // log to console instead

      // TODO: better job of transforming error for user consumption
      //this.log(`${operation} failed: ${error.message}`);

      // Let the app keep running by returning an empty result.
      return of(result as T);
    };
  }

}

cookies.service.ts

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class CookiesService {

  constructor() { }

  getCookie(name: string) {
    let ca: Array<string> = document.cookie.split(';');
    let caLen: number = ca.length;
    let cookieName = `${name}=`;
    let c: string;

    for (let i: number = 0; i < caLen; i += 1) {
      c = ca[i].replace(/^\s+/g, '');
      if (c.indexOf(cookieName) == 0) {
        return c.substring(cookieName.length, c.length);
      }
    }
    return '';
  }

  deleteCookie(name) {
    this.setCookie(name, '', -1);
  }

  setCookie(name: string, value: string, expireDays: number, path: string = '') {
    let d:Date = new Date();
    d.setTime(d.getTime() + expireDays * 24 * 60 * 60 * 1000);
    let expires:string = `expires=${d.toUTCString()}`;
    let cpath:string = path ? `; path=${path}` : '';
    document.cookie = `${name}=${value}; ${expires}${cpath}`;
  }

}

0 个答案:

没有答案