0

The angular-cli version is 6.0.8 and the rxjs version is 6.3.3.

I'm following a tutorial and getting the error: Can't resolve 'rxjs/Rx'. How do I go about resolving the error? Is there a way that I can use a previous version of the rxJS?

Here is the code:

service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable()
export class Service {
  constructor(private _http: HttpClient) {

}

getPosts(): Observable<any> {
    return 
this._http.get("http://jsonplaceholder.typicode.com/posts");
  }

}

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import { Service } from './service';

@NgModule({
 declarations: [
  AppComponent
 ],
 imports: [
  BrowserModule,
  HttpClientModule
 ],
 providers: [HttpClientModule, Service],
 bootstrap: [AppComponent]

}) export class AppModule { }

app.component.ts

import { Component, OnInit } from '@angular/core';
import { Service } from './service';
import  'rxjs/Rx';

@Component({
   selector: 'app-root',
   template: `
    <ul>
      <li *ngFor="let post of _posts">
        <b>{{post.title}}</b> {{post.body}}
      </li>
    </ul>
    <div *ngIf="_error">
      Error: {{_error.status}}:{{_error.statusText}}
    </div>
  `,
   styles: ['div {font-size:20px;padding:5px;background- 
       color:red;color:white}']
})
export class AppComponent implements OnInit {
      _posts = [];
      _error;
      constructor(private _service: Service) {}

      ngOnInit() {
        this._service.getPosts()
            .subscribe(
             response => {
                this._posts = response;
      },
        error => {
          this._error = error;
      }
    );
  }
}
לבני מלכה
  • 15,925
  • 2
  • 23
  • 47
rds80
  • 631
  • 1
  • 10
  • 23

2 Answers2

1

As you are using Rxjs V6 and Above. Rxjs made the path based import optional.

import  'rxjs/Rx';

Why you need this import?. Not seeing RX is used in your component. Please remove and try if not required.

Suresh Kumar Ariya
  • 9,516
  • 1
  • 18
  • 27
0

Make sure that rxjs-compat library also included in pakage.json file

Shiju
  • 438
  • 2
  • 7
  • I didn't end up having to add that library. But in the future, how would I go about adding that in package.json? would it just be "rxjs-compat": "^6.0.0"? – rds80 Oct 07 '18 at 14:55