我试着创建一个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
上导入的服务被评论,因为我还没有使用它,以防万一,出于某种原因我可以解决所有参数,如果有人有想法。
答案 0 :(得分:1)
您获得的错误是与Angular Dependency Injection相关的运行时错误。它无法确定如何创建由第一个构造函数参数表示的依赖项,因为其类型为any
。您可以将类型从any
更改为提供的某种类型,或者在提供的类型或令牌上使用@Inject。角度DI不应该对第二个参数(Router
)有问题。
答案 1 :(得分:0)
您可以使用&#34;任何&#34;在你的构造函数中。构造函数(私有身份验证:任何,私有路由器:路由器)。将其更改为您的服务,如authService。