我的问题听起来很像Cannot find the '@angular/common/http' module和Error loading @angular/common/http - angular 2但问题有点不同: 我正在使用Angular 4.3.5,我试图从Web API读取数据。 (这个API放出JSON数据并使用SignalR和.net Core)。 我已经跟着几个教程,并用此代码为类,将实际联系服务上来:asp.net Core Angular4 angular/common/http找不到
import 'rxjs/add/operator/map';
import { HttpClient, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Configuration } from './Configuration.js';
@Injectable()
export class DataService {
private actionUrl: string;
constructor(private http: HttpClient, private configuration: Configuration) {
this.actionUrl = configuration.serviceUrl;
}
//public getAll<T>(): Observable<T> {
// return this.http.get<T>(this.actionUrl);
//}
public getSingle<T>(id: number): Observable<T> {
return this.http.get<T>(this.actionUrl + id);
}
}
@Injectable()
export class CustomInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (!req.headers.has('Content-Type')) {
req = req.clone({ headers: req.headers.set('Content-Type', 'application/json') });
}
req = req.clone({ headers: req.headers.set('Accept', 'application/json') });
console.log(JSON.stringify(req.headers));
return next.handle(req);
}
}
现在,建设这个项目(我使用Visual Studio 2017年企业)和运行一饮而尽任务transpile对.js的.ts工作得很好,所有的intellisense-tooltips都可以工作 - IDE确实意识到这些东西的存在。 但是,如果我在浏览器中打开它(不事关如果Firefox,边缘或铬)我得到以下错误: zone.js:958 GET http://localhost:3966/libs/@angular/common/bundles/common.umd.js/http 404 (Not Found)
如果我手工编辑transpiled的JavaScript文件,并写入共同-http.umd.js在那里,找到该文件。 (这就是为什么在顶端导入Configuration.js而不是配置的原因 - 它似乎不想自动解析后缀,就像在某些教程中一样)。 我希望我不太模糊,因为这是我第一次公开发问。此外,我无法在给定的问题中找到答案。