简体   繁体   English

Angular 2 http.get

[英]Angular 2 http.get

I am doing the following get in Angular 2 and I am getting a runtime exception: 我正在做以下内容进入Angular 2并且我得到一个运行时异常:

http.get('http://localhost/MyCart/Home/GetProducts').subscribe(result => {
    this.categories = result.json().Data as any[];
    this.LoadDictionary();
    window.console.log(this.categories);
});

Is that the correct syntax? 这是正确的语法吗? What am I doing wrong? 我究竟做错了什么? If I delete the http.get, application is fine. 如果我删除http.get,应用程序就可以了。

EDIT: 编辑:

The error is below. 错误如下。 Keep in mind I am using Angular's SPA template for .NET Core. 请记住,我正在使用Angular的.NET Core模板。

An unhandled exception occurred while processing the request. 处理请求时发生未处理的异常。 Exception: Call to Node module failed with error: ReferenceError: window is not defined at C:\\Users\\samir\\Desktop\\angular-core new\\ClientApp\\dist\\main-server.js:269:13 at SafeSubscriber.schedulerFn [as _next] (C:\\Users\\samir\\Desktop\\angular-core new\\ClientApp\\dist\\vendor.js:3774:40) at SafeSubscriber.module.exports.SafeSubscriber.__tryOrUnsub (C:\\Users\\samir\\Desktop\\angular-core new\\ClientApp\\dist\\vendor.js:23052:16) at SafeSubscriber.module.exports.SafeSubscriber.next (C:\\Users\\samir\\Desktop\\angular-core new\\ClientApp\\dist\\vendor.js:23001:22) at Subscriber.module.exports.Subscriber._next (C:\\Users\\samir\\Desktop\\angular-core new\\ClientApp\\dist\\vendor.js:22943:26) at Subscriber.module.exports.Subscriber.next (C:\\Users\\samir\\Desktop\\angular-core new\\ClientApp\\dist\\vendor.js:22907:18) at EventEmitter.module.exports.Subject.next (C:\\Users\\samir\\Desktop\\angular-core new\\ClientApp\\dist\\vendor.js:68525:25) at Object.next (C:\\Users\\samir\\Desktop\\angular-core new\\ClientApp\\dist\\vendor.js:90000: 例外:调用节点模块失败,错误:ReferenceError:窗口未在C:\\ Users \\ samir \\ Desktop \\ angular-core new \\ ClientApp \\ dist \\ main-server.js:269:13在SafeSubscriber.schedulerFn中定义[as _next](C:\\ Users \\ samir \\ Desktop \\ angular-core new \\ ClientApp \\ dist \\ vendor.js:3774:40)at SafeSubscriber.module.exports.SafeSubscriber .__ tryOrUnsub(C:\\ Users \\ samir \\ Desktop \\ angular -core new \\ ClientApp \\ dist \\ vendor.js:23052:16)在SafeSubscriber.module.exports.SafeSubscriber.next(C:\\ Users \\ samir \\ Desktop \\ angular-core new \\ ClientApp \\ dist \\ vendor.js:23001) :22)在Subscriber.module.exports.Subscriber.next上的Subscriber.module.exports.Subscriber._next(C:\\ Users \\ samir \\ Desktop \\ angular-core new \\ ClientApp \\ dist \\ vendor.js:22943:26) (C:\\ Users \\ samir \\ Desktop \\ angular-core new \\ ClientApp \\ dist \\ vendor.js:22907:18)在EventEmitter.module.exports.Subject.next(C:\\ Users \\ samir \\ Desktop \\ angular-core)在Object.next的新\\ ClientApp \\ dist \\ vendor.js:68525:25)(C:\\ Users \\ samir \\ Desktop \\ angular-core new \\ ClientApp \\ dist \\ vendor.js:90000: 21) at SafeSubscriber.schedulerFn [as _next] (C:\\Users\\samir\\Desktop\\angular-core new\\ClientApp\\dist\\vendor.js:3762:56) at SafeSubscriber.module.exports.SafeSubscriber.__tryOrUnsub (C:\\Users\\samir\\Desktop\\angular-core new\\ClientApp\\dist\\vendor.js:23052:16) at SafeSubscriber.module.exports.SafeSubscriber.next (C:\\Users\\samir\\Desktop\\angular-core new\\ClientApp\\dist\\vendor.js:23001:22) at Subscriber.module.exports.Subscriber._next (C:\\Users\\samir\\Desktop\\angular-core new\\ClientApp\\dist\\vendor.js:22943:26) at Subscriber.module.exports.Subscriber.next (C:\\Users\\samir\\Desktop\\angular-core new\\ClientApp\\dist\\vendor.js:22907:18) at EventEmitter.module.exports.Subject.next (C:\\Users\\samir\\Desktop\\angular-core new\\ClientApp\\dist\\vendor.js:68525:25) at Object.next (C:\\Users\\samir\\Desktop\\angular-core new\\ClientApp\\dist\\vendor.js:90000:21) 21)在SafeSubscriber.chedulerFn [as _next](C:\\ Users \\ samir \\ Desktop \\ angular-core new \\ ClientApp \\ dist \\ vendor.js:3762:56)的SafeSubscriber.module.exports.SafeSubscriber .__ tryOrUnsub(C: \\ Users \\ samir \\ Desktop \\ angular-core new \\ ClientApp \\ dist \\ vendor.js:23052:16)at SafeSubscriber.module.exports.SafeSubscriber.next(C:\\ Users \\ samir \\ Desktop \\ angular-core new \\ ClientApp订阅服务器上的Subscriber.module.exports.Subscriber._next(C:\\ Users \\ samir \\ Desktop \\ angular-core new \\ ClientApp \\ dist \\ vendor.js:22943:26)中的\\ dist \\ vendor.js:23001:22)在EventEmitter.module.exports.Subject.next(C:\\ Users)中的.module.exports.Subscriber.next(C:\\ Users \\ samir \\ Desktop \\ angular-core new \\ ClientApp \\ dist \\ vendor.js:22907:18) \\ samir \\ Desktop \\ angular-core new \\ ClientApp \\ dist \\ vendor.js:68525:25)at Object.next(C:\\ Users \\ samir \\ Desktop \\ angular-core new \\ ClientApp \\ dist \\ vendor.js:90000 :21)

window.console.log(this.categories);

应该

console.log(this.categories);

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

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