【发布时间】:2017-04-24 01:14:59
【问题描述】:
我启动了一个 Angular2 应用程序,但几天以来我一直遇到问题!
Can't resolve all parameters for HomeComponent: (?).(…)
但我的问题不在于特定的提供者:我尝试在 HomeComponent 构造函数中注入的所有内容都会返回该错误。没有关于该错误的类似问题解决了我的情况,现在我花了很多时间寻找这个我找不到它。
App.moodule.ts:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { SimpleNotificationsModule } from 'angular2-notifications';
// Controllers
import { AppComponent } from './app.component';
import { HomeComponent } from './components/home.component';
// Service
import { AuthService } from './services/auth.service';
// Guards
import { AuthGuard } from './guards/auth.guard';
@NgModule({
imports: [
BrowserModule,
RouterModule.forRoot([
{
path: '',
component: HomeComponent,
canActivate: [AuthGuard]
}
]),
SimpleNotificationsModule
],
declarations: [
AppComponent,
HomeComponent
],
providers: [
AuthGuard,
AuthService,
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
我的主页组件:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { NotificationsService } from 'angular2-notifications';
import { AuthService } from '../services/auth.service';
@Component({
selector: 'home',
templateUrl: '/templates/home.html'
})
export class HomeComponent implements OnInit
{
public test: boolean = false;
constructor(
private _authService: AuthService
) {}
ngOnInit()
{
this.test = true;
}
}
我尝试导入的 AuthService 是空的,正如我所说,我在 HomeComponent 中注入的每个提供程序都会返回该错误。每个构造函数参数都会在错误中创建一个新问号。如果您需要更多代码,请告诉我,但我认为其余的(模板、systemjs.config.js ...)不是问题
【问题讨论】:
-
我怀疑导入或项目配置有问题,而不是代码。
-
通过这个但它可能是什么?你想要一个特定的文件吗? Ps : 当我没有在构造函数中添加任何参数时,它工作正常。
-
抱歉,不知道。我自己没用过TS,不了解项目配置。
-
这是指向正确的文件吗?
import { AuthService } from '../services/auth.service'; -
@Hareesh 是的,我的 HomeController 所在的控制器目录与“服务”目录位于同一文件夹中。
标签: angular dependency-injection components