没有 FormBuilder 的提供程序

我仍然是新的,并通过教程学习,但出于某种原因,我得到的消息 No provider for formbuilder时,试图测试唯一的用户名。 我不知道为什么会收到这条信息,但我找不到解决办法。有人能告诉我为什么会这样吗?

Signup-组件

import {Component} from '@angular/core';
import {ControlGroup, Control, Validators, FormBuilder} from '@angular/common'
import {UsernameValidators} from './usernameValidators'


@Component({
selector: 'signup',
templateUrl: 'signup-component.html'


})
export class SignupComponent{
form: ControlGroup;


constructor(fb: FormBuilder){
this.form = fb.group({
username:['', Validators.compose([
Validators.required, UsernameValidators.cannotContainSpace
])],
password: ['', Validators.required]
})
}
}

UsernameValidators.ts

import {Control} from '@angular/common'




export class UsernameValidators{


static shouldBeUnique(control: Control){
return new Promise((resolve, reject) => {
setTimeout(function(){
if(control.value == "andy")
resolve({shouldBeUnique: true});
else
resolve(null);
}, 1000);


});


}




static cannotContainSpace(control: Control){
if (control.value.indexOf(' ') >= 0)
return {cannotContainSpace: true};


return null;
}
}
139966 次浏览

导入 ReactiveFormsModuleFormsModule

@NgModule({
imports: [
BrowserModule /* or CommonModule */,
FormsModule, ReactiveFormsModule
],
...
})

在模块中使用 FormBuilder