未找到模块:错误:Can';t解决';rxjs/add/operator/catch';在';F:\Angular\HttpServices\http services\src\app';
这是我的密码未找到模块:错误:Can';t解决';rxjs/add/operator/catch';在';F:\Angular\HttpServices\http services\src\app';,angular,rxjs,observable,Angular,Rxjs,Observable,这是我的密码 import { injectable } from '@angular/core'; import { Http } from '@angular/http'; import 'rxjs/add/operator/catch'; @Injectable({ providedIn: 'root' }) export class PostsService { private url = 'https://jsonplaceholder.typicode.com/po
import { injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/catch';
@Injectable({
providedIn: 'root'
})
export class PostsService {
private url = 'https://jsonplaceholder.typicode.com/posts';
constructor(private http : Http ) { }
getPosts {
return this.http.get(this.url);
}
deletePost(id) {
return this.http.get(this.url + '/' + id);
}
}
我在我的电脑上做这个代码,它在工作,但在笔记本电脑上不工作。这似乎很有趣,但这是真的。这是我项目的结构为什么不尝试添加“导入”rxjs/add/operators/catch';“(放入“s”并使其成为运算符)而不是“导入”rxjs/add/operator/catch';”rxjs的新版本不再支持单一导入,而是尝试:
import { catchError } from 'rxjs/operators';
在Angular 6 RXJS导入已更改的情况下,必须将运算符“包装”在管道()中,并且在开头写入时不带点(was:.map、.retry、.catch;现在:map、retry和catchError而不是catch): 和使用管道()
“我在我的PC上执行此代码,它在笔记本电脑上工作,但在笔记本电脑上不工作”-在笔记本电脑上,您有一个较新版本的angular cli。它通过新的其他导入生成角度6。在angular 7中,以下是如何以干净的方式处理http get请求
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable, throwError} from 'rxjs';
import {catchError, retry} from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class EmployeeService {
constructor(private _http: HttpClient) {
}
public getPosts(): Observable<Post[]> {
return this._http.get(<URL>).pipe(
retry(1), catchError(error => {
return throwError(error.message);
}));
}
}
从'@angular/core'导入{Injectable};
从'@angular/common/http'导入{HttpClient};
从“rxjs”导入{observatable,throwerr};
从“rxjs/operators”导入{catchError,重试};
@注射的({
providedIn:'根'
})
出口级雇员服务{
构造函数(私有的http:HttpClient){
}
public getPosts():可观察{
返回此。\u http.get().pipe(
重试(1),catchError(错误=>{
返回抛出器(错误消息);
}));
}
}
试试这个,会有用的
从'@angular/core'导入{Injectable};
从“@angular/common/http”导入{HttpClient,HttpErrorResponse};
从“rxjs/operators”导入{catchError};
从“rxjs”导入{observatable,throwerr};
.get().pipe
(
catchError((错误:HttpErrorResponse)=>{
返回抛出器错误(error.message | |'服务器错误');
})
)
如果你来这里是因为最初的代码建议的我的课程
进口
import { throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
这就是它现在的样子
deletePost(id){
return this.http.delete(this.url + '/' + id ).pipe(
catchError(error => {
if (error.status === 404)
return throwError(new NotFoundError());
return throwError(new AppError(error));
}));
}
您是否安装了
npm
以获得所有依赖项可能是不再支持单一导入的rxjs的更新版本。尝试从rxjs/operators中导入{catchError};'是@ShashankVivek我尝试使用npm安装以及npm更新模块“f:/Angular/Angular form/Angular form app/node_modules/rxjs/Observable”没有导出成员“Observable”。现在我发现这个错误Angular CLI:1.7.4和Angular CLI:6.0.1这两者之间有什么区别?虽然这段代码可以回答这个问题,但提供关于如何和/或为什么解决问题的附加上下文将提高答案的长期价值。
import { throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
deletePost(id){
return this.http.delete(this.url + '/' + id ).pipe(
catchError(error => {
if (error.status === 404)
return throwError(new NotFoundError());
return throwError(new AppError(error));
}));
}