Angular 2 - 无法解析组件的所有参数:(?) [英] Angular 2 - Can't resolve all parameters for component: (?)
问题描述
我启动了一个 Angular2 应用程序,但几天以来我一直遇到问题!
I started an Angular2 app and I have an issue since days !
Can't resolve all parameters for HomeComponent: (?).(…)
但我的问题不在于特定的提供者:我尝试在 HomeComponent 构造函数中注入的所有内容都会返回该错误.没有关于该错误的类似问题解决了我的情况,现在我花了很多时间寻找这个我找不到它.
But my issue is not about a particular provider : Everyting that I try to inject in my HomeComponent constructor return that error. No similars questions on that errors resolved my situation, and now I spent so much time looking for this that I can't find it.
App.moodule.ts:
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 ...)不是问题
The AuthService that I try to import is empty, and as I said every providers that I inject in HomeComponent return that error. Each constructor argument create a new question mark in the error. Let me know if you need more code, but I don't think the rest (templates, systemjs.config.js...) is the problem
推荐答案
确保你有
"emitDecoratorMetadata": true,
在您的 tsconfig.js
中.如果启用,您不需要将 @Inject()
添加到函数参数中.
in your tsconfig.js
. You should not need to add @Inject()
to your function parameters if that's enabled.
这篇关于Angular 2 - 无法解析组件的所有参数:(?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!