简体   繁体   English

Angular 2 - 无法解析组件的所有参数:(?)

[英]Angular 2 - Can't resolve all parameters for component: (?)

I started an Angular2 app and I have an issue since days ! 我开始使用Angular2应用程序,自从几天起我就遇到了问题!

Can't resolve all parameters for HomeComponent: (?).(…) 

But my issue is not about a particular provider : Everyting that I try to inject in my HomeComponent constructor return that error. 但我的问题不是特定的提供者:我尝试在HomeComponent构造函数中注入的每一个都返回该错误。 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 { }

My HomeComponent : 我的主页成员:

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;
    }
}

The AuthService that I try to import is empty, and as I said every providers that I inject in HomeComponent return that error. 我尝试导入的AuthService是空的,正如我所说,我在HomeComponent中注入的每个提供程序都返回该错误。 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 如果您需要更多代码,请告诉我,但我不认为其余的(模板,systemjs.config.js ...)是问题

Make sure you have 确保你有

"emitDecoratorMetadata": true,

in your tsconfig.js . 在你的tsconfig.js You should not need to add @Inject() to your function parameters if that's enabled. 如果已启用,则不需要将@Inject()添加到函数参数中。

Import this 导入这个

import {Inject} from '@angular/core';

And change your constructor to 并将构造函数更改为

constructor(@Inject(AuthService) _authService: AuthService) 
{

}

you should @Inject any service into constructor before using it. 在使用它之前,你应该@Inject任何服务到构造函数。

and your service should be injectable 并且您的服务应该是可注射的

@Injectable()
export class AuthService {

}

My problem ended up being nothing. 我的问题最终没有成功。 I simply restarted the webpack watcher and then everything was fine. 我只是重新启动了webpack观察者,然后一切都很好。 For reference, I'm using the Angular CLI. 作为参考,我正在使用Angular CLI。

The issue that raised this error for me was completely different. 为我提出这个错误的问题完全不同。 I had a logging category in the component that I had mistakenly used in the service, and thus the service had a dependency on the component and wasn't able to be created in time for it to be injected into the component. 我在服务中错误地使用了组件中的日志记录类别,因此服务依赖于组件,并且无法及时创建它以将其注入组件。

TL;DR: Check the imports on the service that can't be injected. TL; DR:检查无法注入的服务上的导入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM