【发布时间】:2018-06-22 07:31:08
【问题描述】:
我在做https://coursetro.com/posts/code/126/Let's-build-an-Angular-5-Chart.js-App---Tutorial
并面临一些问题。 目前我正在使用:
Angular CLI: 6.0.8
Node: 10.4.1
OS: linux x64
Angular: 6.0.6
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.6.8
@angular-devkit/build-angular 0.6.8
@angular-devkit/build-optimizer 0.6.8
@angular-devkit/core 0.6.8
@angular-devkit/schematics 0.6.8
@angular/cli 6.0.8
@ngtools/webpack 6.0.8
@schematics/angular 0.6.8
@schematics/update 0.6.8
rxjs 6.2.1
typescript 2.7.2
webpack 4.8.3
如果我想使用,weather.service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import 'rxjs/add/operator/map';
@Injectable({
providedIn: 'root'
})
export class WeatherService {
constructor(private _http: HttpClient) { }
dailyForecast() {
return this._http.get("http://samples.openweathermap.org/data/2.5/history/city?q=Warren,OH&appid=b6907d289e10d714a6e88b30761fae22").map(result => result);
}
}
但它给了我一个错误:
(浏览器控制台)
请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http://localhost:4200' 因此不允许访问。
(终端)
./src/app/weather.service.ts 中的错误 未找到模块:错误:无法解析“/home/nodira/AngularProjects/charts/src/app”中的“rxjs/add/operator/map”
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class WeatherService {
constructor(private _http: HttpClient) { }
dailyForecast() {
return this._http.get("http://samples.openweathermap.org/data/2.5/history/city?q=Warren,OH&appid=b6907d289e10d714a6e88b30761fae22").pipe(map(result => result));
}
}
现在我在终端中没有收到任何错误,但在浏览器的控制台中也没有看到任何所需的result。
当我想使用filter 时,我遇到了类似的Can't resolve rxjs/add/operator/filter 错误。我将衷心感谢您的帮助。
先谢谢了。
【问题讨论】:
-
您希望执行实际 console.log() 的代码在哪里?
-
它在 app.component:
constructor(private _weather: WeatherService) { } ngOnInit() { this._weather.dailyForecast().subscribe(res => { console.log(res); }) }我正在将 weather.service 注入 app.comonent
标签: dictionary rxjs http-get angular6