Angular 2 http.get

Angular 2 http.get

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

这是正确的语法吗?我究竟做错了什么?如果我删除 http.get,应用程序没问题。

编辑:

错误如下。请记住,我正在使用 Angular 的 .NET Core SPA 模板。

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: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)

window.console.log(this.categories);

应该是

console.log(this.categories);