我无法理解我的代码出了什么问题,我导入了模块,但仍然收到此错误“未找到名称‘matAutocomplete’导出”
注意:导入MatAutocompleteModule后,我重新启动IDE再次编译几次,仍然一样。
这是 app.module.ts - 仅粘贴代码的相关部分
import {MatAutocompleteModule} from '@angular/material/autocomplete';
@NgModule({ declarations:[..xxxx....],
imports:[xx,xxx,MatAutocompleteModule],
providers:[xx,xx],
bootstrap: [AppComponent]})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
组件.html
<form class="form-group-parent p-0" [formGroup]="frmStep1" (ngSubmit)="submit()">
<mat-form-field class="example-full-width">
<input type="text" placeholder="Ex. English" aria-label="Language" matInput
[formControl]="language" [matAutocomplete]="auto">
<mat-autocomplete autoActiveFirstOption #auto="matAutocomplete">
<mat-option *ngFor="let option of languageList | async" [value]="option">
{{option}}
</mat-option>
</mat-autocomplete>
<mat-error>
<strong>Language</strong> is required.
</mat-error>
</mat-form-field>
</form>
Run Code Online (Sandbox Code Playgroud)
组件.ts
import {Component, OnInit} from '@angular/core';
import {FormControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
import {Observable} from 'rxjs';
import {map, startWith} from 'rxjs/operators';
@Component({
selector: 'xxxx',
templateUrl: './cxx.component.html',
styleUrls: ['./xxx.component.scss']
})
export class xxxComponent implements OnInit {
constructor(
private pageTitle: Title,
private router: Router,
private fb: FormBuilder,
private http: HttpClient,
) { this.createForm(); }
frm1:FormGroup;
frm2:FormGroup;
languageList: Array<any> = ['A','B','C','D','E']
filteredOptions: Observable<string[]>;
ngOnInit():void{
this.filteredOptions = this.frmStep2.valueChanges.pipe(
startWith(''),
map(value => this._filter(value))
);
}
createForm(): object {
this.frmStep1 = this.fb.group({
language:[,[Validators.required]]
});
return frmStep1 :this.frmStep1
}
}
Run Code Online (Sandbox Code Playgroud)
DGR*_*DGR 11
好吧,我需要在特定规范文件的“声明”下添加“MatAutoComplete”。就你而言,我想它应该在“component.spec.ts”中。
我就是这样做的
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ ... , MatAutocomplete ],
providers: [ ... ],
})
.compileComponents();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8911 次 |
| 最近记录: |