gpt4 book ai didi

Angular 7 测试 : "Can' t bind to formGroup since it isn't a known property of form"

转载 作者:太空狗 更新时间:2023-10-29 19:31:54 24 4
gpt4 key购买 nike

控制台记录错误:

15 02 2019 14:50:24.868:INFO [Chrome 72.0.3626 (Windows 10.0.0)]: Connected on socket BiEioS9fHwq-QLg3AAAA with id 27946068 Chrome 72.0.3626 (Windows 10.0.0) LoginComponent should create FAILED Can't bind to 'formGroup' since it isn't a known property of 'form'. ("

        <div class="row">

(etc)

我正在使用命令 ng test 运行它。我的规范文件:

describe('LoginComponent', () => {

let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;

const fakeActivatedRoute = {
snapshot: { data: {} }
} as ActivatedRoute;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
LoginComponent
],
imports: [
RouterTestingModule,
HttpClientModule,
CommonModule,
BrowserModule,
BrowserAnimationsModule,
ReactiveFormsModule,
MessageModule,
MatFormFieldModule,
MatInputModule,
MatButtonModule,
MatCheckboxModule,
MatProgressSpinnerModule,
MatRadioModule,
MatSliderModule,
NgbModule
],
providers: [
{
provide: ActivatedRoute,
useValue: fakeActivatedRoute
}
]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(LoginComponent);
component = fixture.debugElement.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});

});

LoginModule 文件:

@NgModule({
declarations: [LoginComponent],
imports: [
CommonModule,
BrowserModule,
BrowserAnimationsModule,
FormsModule,
ReactiveFormsModule,
MatFormFieldModule,
MatInputModule,
MatButtonModule,
MatCheckboxModule,
MatProgressSpinnerModule,
MatRadioModule,
MatSliderModule,
MessageModule,
NgbModule.forRoot()
],
providers: [],
bootstrap: [
LoginComponent,
MessageComponent
]
})
export class LoginModule {}

缺少什么?

最佳答案

这取决于您在项目中使用的表单类型。 Angular 提供了模板驱动的表单和响应式表单。如果您正在使用 Reactive 表单,则需要将 ReactiveFormsModule 导入您的 componentName.spec.ts 文件中作为

import { ReactiveFormsModule } from '@angular/forms';
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ContactUsFormComponent ],
imports: [ReactiveFormsModule]
})
.compileComponents();}));

否则,如果您正在使用模板驱动的表单,则需要导入 FormsModule。

关于 Angular 7 测试 : "Can' t bind to formGroup since it isn't a known property of form",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54713782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com