我有一个关于 Angular 5 组件的问题,我的组件如下:
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AuthService } from './../auth/auth.service';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css'],
providers: [FormBuilder, AuthService] // is this line needed?
})
export class LoginComponent implements OnInit {
constructor(private fb: FormBuilder, private authService: AuthService) { }
ngOnInit() {
}
}
如果没有加上 providers: [FormBuilder, AuthService] 这一行,我会得到以下异常:
NullInjectorError: No provider for FormBuilder!
如果我添加 providers: [FormBuilder, AuthService] 那么一切都正常工作。我的问题是,这行代码真的必要吗?因为我在教程中看到了没有这行代码的组件(例如 Creating the Login component)。