我正在使用ChangeDetectorRef在网络请求后更新视图。
import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { QRCodeModule } from 'angularx-qrcode';
@Component({
selector: 'app-print',
templateUrl: './print.component.html',
styleUrls: ['./print.component.css'],
})
export class PrintComponent implements OnInit {
barcodeitems;
selecteditems = [];
constructor(public http: HttpClient, private cdr: ChangeDetectorRef){
}
ngOnInit(): void {
}
getqr() {
console.log("selecteditems array", this.selecteditems);
let filterQuery = this.selecteditems.map(i => `ID eq '${i}'`).join(" or ");
let url = `https://example.com/corporate/dev/_api/web/lists/getbytitle('Document Separator Barcodes')/items?$top=1000&$orderBy=ID&$filter=${filterQuery}`
this.http.get(url).subscribe(data => {
this.barcodeitems = data['value'];
this.cdr.detectChanges();
});
}
}
使用ng serve
,我收到错误:Error: StaticInjectorError(AppModule)[PrintComponent -> ChangeDetectorRef]: StaticInjectorError(Platform: core)[PrintComponent -> ChangeDetectorRef]: NullInjectorError: No provider for ChangeDetectorRef!
。
我尝试在app.module.ts中将ChangeDetectorRef
添加到providers
,如下所示:
import { NgModule, ChangeDetectorRef } from '@angular/core';
.....
import { AppComponent } from './app.component';
import { PrintComponent } from './print/
print.component';
@NgModule({
declarations: [
AppComponent,
PrintComponent,
],
imports: [
BrowserModule,
HttpClientModule,
QRCodeModule,
FormsModule,
],
providers: [PrintComponent, ChangeDetectorRef],
bootstrap: [AppComponent]
})
export class AppModule { }
这给了我一个错误:
Argument of type '{ declarations: (typeof AppComponent | typeof PrintComponent)[]; imports: (typeof BrowserModule |...' is not assignable to parameter of type 'NgModule'. Types of property 'providers' are incompatible. Type '(typeof PrintComponent | typeof ChangeDetectorRef)[]' is not assignable to type 'Provider[]'. Type 'typeof PrintComponent | typeof ChangeDetectorRef' is not assignable to type 'Provider'. Type 'typeof ChangeDetectorRef' is not assignable to type 'Provider'. Type 'typeof ChangeDetectorRef' is not assignable to type 'FactoryProvider'. Property 'provide' is missing in type 'typeof ChangeDetectorRef'.
如何将ChangeDetectorRef
添加为提供者?
答案 0 :(得分:-1)
为什么要提供PrintComponent?您无法提供组件。...从模块中完全删除提供程序,它应该可以工作。