I found this example code in a tutorial:
getRandomQuote() {
this.http.get('http://localhost:3001/api/random-quote')
.map(res => res.text())
.subscribe(
data => this.randomQuote = data,
err => this.logError(err),
() => console.log('Random Quote Complete')
);
}
But when trying to use it, I only get TypeError: this.http.get(...).map is not a function in [null]
:
getChannels():Promise<Channel> {
return this.http.get('...')
.map(function (response:Response) {
...
}).toPromise();
}
My Typescript compiler tells me that those methods are avaible but when inspecting the return value of http.get()
they are missing.
I used the package.json of the current angualar2 starting guide:
"dependencies": {
"angular2": "2.0.0-beta.0",
"systemjs": "0.19.6",
"es6-promise": "^3.0.2",
"es6-shim": "^0.33.3",
"reflect-metadata": "0.1.2",
"rxjs": "5.0.0-beta.0",
"zone.js": "0.5.10"
},
...
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="node_modules/angular2/bundles/http.dev.js"></script>
Any ideas what I might get wrong at this point?
Using Map operator in Angular Syntax. The syntax of the map operator is as follows. The project can accept two arguments. one is value i.e. the value... Using Observable Map. To use map first we need to import it from the rxjs/operators library. Next, create an observable... Map Examples. The ...
The @angular/http module has been removed in recent versions of Angular and replaced by @angular/common/http . Furthermore, there are some name changes that might need manual fixing, including:
There are multiple ways to handle multiple requests; they can be sequential or in parallel. In this post, we will cover both. Let's start with a simple HTTP request using the Angular Http service. In our app, we have just a single component that pulls in Angular's Http service via Dependency Injection.
This at least proves that you are getting back an observable with the correct data. subscribe is not considered an operator, but rather a method of any observable instance. Thus it can never be missing. Sorry, something went wrong. IMO This really needs to be added to the angular2 documentation ( import rxjs/Rx or importing operators 1 by 1).
Observable
by default comes with just a few operators. You have to explicitly import them:
import 'rxjs/add/operator/map';
or, if you don't wanna think about it, just load everything (in your bootstrap file, for example):
import 'rxjs/Rx';
You will want your index.html to look something like this, so system.js can find all the rxjs dependencies that you import in your components.
<script src="/lib/anguar2/angular2-polyfills.js"></script>
<script src="/lib/es6-shim/es6-shim.js"></script>
<script src="/lib/systemjs/system.src.js"></script>
<script>
System.config({
defaultJSExtensions: true,
packages: {
app: {
format: 'register'
}
},
map: {
'rxjs':"lib/rxjs"
}
});
</script>
<script src="/lib/anguar2/angular2.dev.js"></script>
<script src="/lib/anguar2/router.dev.js"></script>
<script src="/lib/anguar2/http.js"></script>
<script>
System.import('app/boot');
</script>
Now you can use this in your boot.ts file:
import 'rxjs/add/operator/map';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With