Angular auth guard 无法解析所有参数

Angular auth guard Can't resolve all parameters

我正在尝试创建一个 auth.guard.service 但由于某种原因我收到错误 Can't resolve all parameters for AuthGuard: (?, [object Object]).,这是服务。

import { Observable } from 'rxjs/Observable';
// import { AuthService } from './auth.service';
import { Injectable } from '@angular/core';
import { CanActivate, Router, RouterStateSnapshot, ActivatedRouteSnapshot } from '@angular/router';

@Injectable()
export class AuthGuard implements CanActivate {
  user = true;
  constructor(private auth: any, private router: Router) { }

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | boolean {
    if (this.user) {
      return true;
    }
    this.router.navigate(['']);
    return false;
  }
}

这里是 app.module

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { NavComponent } from './components/nav/nav.component';
import { HomeComponent } from './components/home/home.component';
import { GalleryComponent } from './components/gallery/gallery.component';
import { FooterComponent } from './components/footer/footer.component';

import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { ScrollToModule } from 'ng2-scroll-to';
import { NgxCarouselModule } from 'ngx-carousel';
import { AnimateOnScrollModule } from 'ng2-animate-on-scroll';

import { AuthGuard } from './services/auth.guard.service';

import { HttpClientModule, HttpClient } from '@angular/common/http';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';


const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'gallery', component: GalleryComponent, canActivate: [AuthGuard] }
];

export function HttpLoaderFactory(http: HttpClient) {
  return new TranslateHttpLoader(http);
}

@NgModule({
  declarations: [
    AppComponent,
    NavComponent,
    HomeComponent,
    GalleryComponent,
    FooterComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    NgxCarouselModule,
    NgbModule.forRoot(),
    ScrollToModule.forRoot(),
    RouterModule.forRoot(routes),
    AnimateOnScrollModule.forRoot(),
    HttpClientModule,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: HttpLoaderFactory,
        deps: [HttpClient]
      }
    })
  ],
  exports: [
    TranslateModule
  ],
  providers: [AuthGuard],
  bootstrap: [AppComponent]
})
export class AppModule { }

我不知道哪里可能出错,我在 auth.guard.services 上导入的服务被评论了,因为我还没有使用它,以防万一,出于某种原因,如果有人,我可以解析所有参数有个想法。

您收到的错误是与 Angular 依赖注入相关的 运行 时间错误。它无法确定如何创建由第一个构造函数参数表示的依赖项,因为它的类型是 any。您可以将类型从 any 更改为提供的某种类型,或者对提供的类型或标记使用 @Inject。 AngularDI第二个参数应该没有问题(Router).

您可以在构造函数中使用 "any"。构造函数(私人授权:any,私人路由器:路由器)。将其更改为您的服务,例如 authService。