使用Angular 6生成库后,无法解析组件全部参数问题求助
Hey there! Let's break down this issue you're facing with your Angular 6 library service. That "can't resolve all parameters" error almost always ties back to dependency injection misconfiguration—let's walk through the most likely fixes step by step.
1. Make Sure HttpClientModule is Imported in Your Library Module
Your service relies on HttpClient, which means your library's NgModule needs to import HttpClientModule so the Angular injector can find it. If you skip this, the injector won't know how to provide HttpClient to your service, leading to the parameter resolution error.
Update your library module like this:
import { NgModule, ModuleWithProviders } from '@angular/core'; import { HttpClientModule } from '@angular/common/http'; import { AuthService } from './auth.service'; @NgModule({ imports: [HttpClientModule], // Critical: Import HttpClientModule here declarations: [], exports: [] }) export class AuthLibraryModule { // Optional but recommended: Use forRoot() to provide services when importing the library static forRoot(): ModuleWithProviders { return { ngModule: AuthLibraryModule, providers: [AuthService] }; } }
2. Fix Your @Injectable Decorator Configuration
In Angular 6, the @Injectable decorator works best with either the providedIn property or explicit declaration in your module's providers array (pick one—don't do both).
Option A: Use providedIn: 'root' (Global Service)
This makes your service available app-wide without needing to add it to every module's providers:
import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; // ... other imports @Injectable({ providedIn: 'root' // Add this line }) export class AuthService { // ... your service logic }
Option B: Declare in Library Module Providers
If you want to keep the service scoped to your library, add it to your module's providers array (as shown in step 1's code example).
3. Verify Public API Exports
For your service to be accessible outside the library, you need to export it via the library's public-api.ts file. This tells Angular which parts of your library are exposed to consuming apps:
// public-api.ts export * from './lib/auth.service'; export * from './lib/auth-response.model'; export * from './lib/auth-library.module';
4. Check Consuming Component/Module Setup
If you're using the service in a component outside the library:
- Make sure the consuming module imports your library module (use
AuthLibraryModule.forRoot()in your AppModule for root-level services) - Double-check that your component's constructor injects the service correctly (no typos, correct import path):
import { AuthService } from 'your-auth-library'; // Use the library's package name here constructor(private authService: AuthService) {} // No typos in service name
5. Fix Missing Type Definitions (for jwt-decode)
You're using jwt-decode—make sure you've installed its TypeScript types to avoid implicit any errors that could throw off the injector:
npm install @types/jwt-decode --save-dev
Quick Checklist to Rule Out Small Issues
- No circular imports between your service, models, or module
- All dependencies (like
@angular/common,rxjs) are listed aspeerDependenciesin your library'spackage.json - You've rebuilt the library (
ng build your-library-name) after making configuration changes
That should cover the most common causes of this parameter resolution error. Let me know if you hit any snags while trying these fixes!
内容的提问来源于stack exchange,提问作者KellyM

