使用Angular 6 Generate Library后无法解析Component的所有参数

时间:2018-05-08 20:03:35

标签: angular

我开始了一个新的Angular 6项目并决定尝试新的ng generate library命令。

此库的“主要”功能是服务:

    import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { AuthResponseModel } from  "./auth-response.model";
import { catchError, map } from 'rxjs/operators';
import { HttpErrorResponse } from '@angular/common/http';
import  decode from 'jwt-decode'
@Injectable()
export class AuthService {
  private authURL: string = "http://localhost:8090/oauth/token";
  private loginPath : string = "/login";
  isLoggedIn: boolean = false;
  redirectURL: string;
  public isRefreshing : boolean = false;

  constructor(private http: HttpClient) {
    this.redirectURL = "";
    this.isLoggedIn = this.getToken() != null;

  }
  login(user: string, password: string): Observable<boolean> {
    var data = new FormData();
    data.append("grant_type", "password");
    data.append("username", user);
    data.append("password ", password);

    const httpOptions = {
      headers: new HttpHeaders({
        'Authorization': 'Basic ' + window.btoa("web:secret")
      })
    };

    return this.http.post<AuthResponseModel>(this.authURL, data, httpOptions)
      .pipe(
        map((r: AuthResponseModel) => {
          if (r.access_token) {
            localStorage.setItem("access_token", r.access_token);
            localStorage.setItem("refresh_token", r.refresh_token);
            this.isLoggedIn = true;
            return true;
          } 
        }
        ));
  };

  refresh() : Observable<any> {
    var data = new FormData();
    this.isRefreshing = true;
    data.append("grant_type", "refresh_token");
    data.append("refresh_token", this.getRefreshToken());
    const httpOptions = {
      headers: new HttpHeaders({
        'Authorization': 'Basic ' + window.btoa("web:secret")
      })
    };

   return this.http.post<AuthResponseModel>(this.authURL, data, httpOptions)
    .pipe(
     map( r => {
       console.log("Old token" + this.getToken());
        localStorage.setItem("access_token", r.access_token);
        localStorage.setItem("refresh_token", r.refresh_token);
        this.isLoggedIn = true;
        this.isRefreshing = false;
        console.log("New token" + r.access_token);
        return r.access_token;
      })
    )

  }

  logout(): void {
    localStorage.removeItem("access_token");
    localStorage.removeItem("refresh_token");
    this.isLoggedIn = false;
  }

  checkTokenExpired(checkRefresh = false) : boolean {
    if(checkRefresh) {  return decode(this.getRefreshToken()).exp < (Date.now().valueOf() / 1000); }
    return decode(this.getToken()).exp < (Date.now().valueOf() / 1000);
  }
  getToken(): string {
    return localStorage.getItem("access_token");
  }

  getRefreshToken() : string {
    return localStorage.getItem('refresh_token');
  }


}

根据ng generate library生成的模板代码,我的public_api.ts代码如下:

    /*
 * Public API Surface of auth-lib
 */

export * from './lib/auth-lib.service';
export * from './lib/auth-interceptor';
export * from './lib/auth-response.model';

但是,无论何时我尝试将其注入@Component

    import { Component, OnInit } from '@angular/core';
import { AuthService } from 'auth-lib';
import { Router } from '@angular/router';
import {Injectable} from '@angular/core';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})

export class LoginComponent implements OnInit {

  user : string;
  pass : string;

  constructor(private authService : AuthService, private router : Router) {
    this.user = "";
    this.pass = "";
   }

我收到以下内容:

[Angular] Can't resolve all parameters for LoginComponent

问题与注入AuthService有关(删除它会删除错误)。我引用了StackOverflow的几个问题(包括thisthisthis one);然而,决议似乎围绕“桶”而忘记@Injectable注释。因此,我不确定如何将这些解决方案应用于我的问题。

任何建议都将受到赞赏。

感谢。

编辑:

有趣的是,如果我将导入语句从import { AuthService } from 'auth-lib'更改为import { AuthService } from 'projects/auth-lib/src/public_api';,它确实有效,但这并不是理想的行为。

0 个答案:

没有答案