简体   繁体   English

找不到asp.net Core Angular4 angular / common / http

[英]asp.net Core Angular4 angular/common/http not found

My question sounds similar to Cannot find the '@angular/common/http' module and Error loading @angular/common/http - angular 2 but the problem is a bit different: I am using Angular 4.3.5 and I am trying to read data from a Web API. 我的问题听起来类似于找不到'@ angular / common / http'模块加载@ angular / common / http时出错-angular 2,但问题有点不同:我使用的是Angular 4.3.5,我正在尝试阅读来自Web API的数据。 (This API puts out JSON data and is using SignalR and .net Core). (此API发出JSON数据,并使用SignalR和.net Core)。 I have followed several tutorials and came up with this code for the class that will actually contact the service: 我遵循了一些教程,并为将实际联系该服务的类提供了以下代码:

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);
  }
}

Now, building this project (I am using Visual Studio 2017 Enterprise) and running a gulp task to transpile the .ts to .js works just fine, so do all the intellisense-tooltips - the IDE does recognize the existance of those things. 现在,构建这个项目(我正在使用Visual Studio 2017 Enterprise)并运行一个吞咽任务将.ts转换为.js可以正常工作,所有intellisense-tooltips也是如此-IDE可以识别这些东西的存在。 But if I open it up in a browser (doesnt matter if firefox, edge or chrome) I get the following error: zone.js:958 GET http://localhost:3966/libs/@angular/common/bundles/common.umd.js/http 404 (Not Found) 但是,如果我在浏览器中打开它(无论是Firefox,edge还是chrome),都会收到以下错误:zone.js:958 GET http://localhost:3966/libs/@angular/common/bundles/common.umd.js/http 404 (Not Found)

If I edit the transpiled javascript file by hand and write common-http.umd.js there, the file is found. 如果我手动编辑转译的javascript文件并在其中写入common-http.umd.js,则会找到该文件。 (This is the reason why at the top I import Configuration.js instead of Configuration - it doesnt seem to want to automatically resolve the suffix, like in some tutorials). (这就是为什么我在顶部导入Configuration.js而不是Configuration的原因-它似乎不想像某些教程中那样自动解决后缀)。 I hope I am not too vague, since this is my first Time asking something publically. 我希望我不要太含糊,因为这是我第一次公开询问问题。 Also I was not able to find an answer in the given questions. 我也无法在给定的问题中找到答案。

Well, I found a solution, for anybody who is curious why this and similar problems exist: I had to edit my systemjs file and add this line: 好吧,我找到了一个解决方案,对于任何好奇为什么存在此问题和类似问题的人:我必须编辑systemjs文件并添加以下行:

'@angular/common/http': 'npm:@angular/common/bundles/common-http.umd.js',

and it works! 而且有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM