Angular CLI中Observable导入报错及旧版本项目创建咨询
Hey there! Let's work through your two issues step by step—first the import error you're seeing with Observable, then how to create a project using an older Angular CLI version.
1. Fixing the Observable Import Error (Angular 6)
Angular 6 uses RxJS 6.x, which changed how you import Observable and operators from the library. The old path rxjs/Observable is deprecated in this version. Here's what you need to do instead:
- For the base Observable class, import directly from
rxjs:import { Observable } from 'rxjs'; - If you're using operators like
map,filter, orswitchMap, you'll now import them fromrxjs/operatorsand use them with thepipe()method (instead of chaining them directly):import { map, filter } from 'rxjs/operators'; // Example usage myData$.pipe( map(data => data.transformed), filter(data => data.isValid) ).subscribe(result => { /* handle result */ });
This should resolve the red underline error you're seeing in your code.
2. Creating a Project with an Older Angular CLI Version (e.g., Angular 6)
To create a project using a specific older version of Angular CLI, follow these steps:
Step 1: Uninstall existing global Angular CLI (optional but recommended)
If you have a newer version of Angular CLI installed globally, it's best to uninstall it first to avoid conflicts:
npm uninstall -g @angular/cli
Step 2: Install the specific older CLI version
For Angular 6, you'll want to install the latest stable version of CLI 6.x (which is 6.2.9). Run this command:
npm install -g @angular/cli@6.2.9
You can replace 6.2.9 with any specific version number you need—just make sure it matches the Angular version you want to use.
Step 3: Create your project
Once the CLI is installed, create your project as usual:
ng new my-angular6-project
This will generate a project with Angular 6 dependencies matching the CLI version you installed.
Alternative: Use npx to avoid global installation
If you don't want to install the older CLI globally, you can use npx to run it directly:
npx @angular/cli@6.2.9 new my-angular6-project
This creates the project using the specified CLI version without modifying your global setup.
内容的提问来源于stack exchange,提问作者Maulik Modi

