gpt4 book ai didi

angular - Angular 6 中的 NullInjectorError : No provider for String!

转载 作者:太空狗 更新时间:2023-10-29 18:36:53 24 4
gpt4 key购买 nike

父类

import { BadRequestError } from './../common/bad-request-error';
import { NotFoundError } from './../common/not-found-error';
import { AppError } from './../common/app-error';
import { Http } from '@angular/http';
import { Injectable, OnInit } from '@angular/core';
import { catchError } from 'rxjs/operators';
import { throwError} from 'rxjs';

@Injectable({
providedIn: 'root'
})
export class DataService {
constructor(private url: string , private http: Http) {
}

getAll() {
return this.http.get(this.url).pipe(catchError(this.handleError));
}

delete(id) {
return this.http.delete(this.url + '/' + id)
.pipe(
catchError(this.handleError));
}

update(resource) {
return this.http.patch(this.url + '/' + resource.id,
JSON.stringify({isRead: true})).pipe(
catchError(this.handleError));
}

create(resource) {
return this.http.post(this.url , JSON.stringify(resource))
.pipe(
catchError(this.handleError)
);

}

private handleError(err: Response) {
if (err.status === 404) {
return throwError(new NotFoundError());
} if (err.status === 400) {
return throwError(new BadRequestError(err.json()));
}
return throwError(new AppError(err));
}
}

子类

 import { DataService } from './data.service';
import { Http } from '@angular/http';
import { Injectable } from '@angular/core';

@Injectable({
providedIn: 'root'
})
export class PostService extends DataService {
constructor(http: Http) {
super('https://jsonplaceholder.typicode.com/posts' , http);
}

}

从子类传递字符串时出现以下错误。

Error: StaticInjectorError(AppModule)[String]:
StaticInjectorError(Platform: core)[String]: NullInjectorError: No provider for String! at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get (core.js:1062) at resolveToken (core.js:1300) at tryResolveToken (core.js:1244) at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:1141) at resolveToken (core.js:1300) at tryResolveToken (core.js:1244) at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:1141) at resolveNgModuleDep (core.js:8376) at NgModuleRef_.push../node_modules/@angular/core/fesm5/core.js.NgModuleRef_.get (core.js:9064) at inject (core.js:1403)

请建议如何解决上述错误。

最佳答案

任何添加到服务 Angular 参数都会尝试通过 DI 系统注入(inject)它。 DataService 在这种情况下考虑 api 服务的基类,我们不需要让它 injectable。所以只需删除可注入(inject)装饰器

数据服务

export class DataService {

constructor(private url: string , private http: Http) {
}
...

}

关于angular - Angular 6 中的 NullInjectorError : No provider for String!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52049910/

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