gpt4 book ai didi

angular - 无法将 ChangeDetectorRef 添加为提供者

转载 作者:太空狗 更新时间:2023-10-29 17:21:12 25 4
gpt4 key购买 nike

我正在使用 ChangeDetectorRef 在网络请求后更新 View 。

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:没有 ChangeDetectorRef 的提供者!

我尝试将 ChangeDetectorRef 添加到 app.module.ts 中的 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 添加为提供者?

最佳答案

为什么要提供 PrintComponent?你不能提供一个组件....从你的模块中完全删除提供者,它应该可以工作。

关于angular - 无法将 ChangeDetectorRef 添加为提供者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48910362/

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