You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular CLI中Observable导入报错及旧版本项目创建咨询

Fixing Observable Import Error & Creating Projects with Older Angular CLI Versions

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, or switchMap, you'll now import them from rxjs/operators and use them with the pipe() 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:

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:18:30