简体   繁体   English

RxJS在浏览器中缺少运算符错误,但在Visual Studio Code中不存在

[英]RxJS missing operator errors in browser but not in the Visual Studio Code

I have strange problem. 我有一个奇怪的问题。 I had existing Angular4 project managed under Angular-CLI with RxJS library. 我有使用RxJS库在Angular-CLI下管理的现有Angular4项目。 After updating RxJS library to version 5.5.2 project starts to have problems with Observable operators. RxJS库更新到5.5.2版后,项目开始出现Observable运算符的问题。 The Errors in Google Chrome are like this: Google Chrome中的错误是这样的:

ERROR TypeError: this.apiService.request(...).switchMap is not a function

I know I can fix this with: 我知道我可以通过以下方法解决此问题:

import 'rxjs/add/operator/switchMap';

But problem is with IDE in this case Visual Studio Code . 但是在这种情况下, Visual Studio Code是IDE的问题。 It just not shows which operator imports missing. 它只是不显示缺少哪个操作员输入。 The only way I found so far to detect missing operators is to run application and check all features of the app in the browser with Developer Console opened waiting for this kind of errors. 到目前为止,我发现发现缺少操作员的唯一方法是运行应用程序,并在打开Developer Console在浏览器中检查应用程序的所有功能,以等待此类错误。 So I have two questions. 所以我有两个问题。

  • Is there any better way to detect missing operators in my updated project? 有什么更好的方法来检测更新项目中缺少的运算符? Why doesn't the build process catch the fact that the operator is missing? 为什么构建过程没有捕捉到操作员缺失的事实?
  • Is there any way that I can setup Visual Studio Code to show me this kind of errors before I push some changes and discover them in the browser? 在推送某些更改并在浏览器中发现它们之前,有没有什么方法可以设置Visual Studio Code向我显示此类错误? Syntax completion in this IDE is even working with not imported operators, which I think is wrong. 此IDE中的语法补全甚至可以与未导入的运算符一起使用,我认为这是错误的。

I'm attaching output from ng -v : 我正在附加ng -v输出:

    _                      _                 ____ _     ___
   / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
  / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
 / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
/_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
               |___/

Angular CLI: 1.5.3
Node: 6.11.2
OS: darwin x64
Angular: 4.4.6
... animations, common, compiler, compiler-cli, core, forms
... http, platform-browser, platform-browser-dynamic
... platform-server, router, tsc-wrapped

@angular/cdk: 2.0.0-beta.12
@angular/cli: 1.5.3
@angular/flex-layout: 2.0.0-beta.8
@angular/material: 2.0.0-beta.12
@angular-devkit/build-optimizer: 0.0.33
@angular-devkit/core: 0.0.21
@angular-devkit/schematics: 0.0.37
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.8.3
@schematics/angular: 0.1.7
typescript: 2.3.4
webpack: 3.8.1

And output from npm list --depth=0 并从npm list --depth=0输出npm list --depth=0

├── @angular/animations@4.4.6
├── @angular/cdk@2.0.0-beta.12
├── @angular/cli@1.5.3
├── @angular/common@4.4.6
├── @angular/compiler@4.4.6
├── @angular/compiler-cli@4.4.6
├── @angular/core@4.4.6
├── @angular/flex-layout@2.0.0-beta.8
├── @angular/forms@4.4.6
├── @angular/http@4.4.6
├── @angular/material@2.0.0-beta.12
├── @angular/platform-browser@4.4.6
├── @angular/platform-browser-dynamic@4.4.6
├── @angular/platform-server@4.4.6
├── @angular/router@4.4.6
├── @types/hammerjs@2.0.34
├── @types/jasmine@2.5.45
├── @types/node@6.0.78
├── ag-grid@13.3.1
├── ag-grid-angular@13.3.0
├── classlist.js@1.1.20150312
├── codelyzer@3.0.1
├── core-js@2.4.1
├── hammerjs@2.0.8
├── i18n-iso-countries@3.2.2
├── jasmine-core@2.6.4
├── jasmine-spec-reporter@4.1.1
├── karma@1.7.0
├── karma-chrome-launcher@2.1.1
├── karma-cli@1.0.1
├── karma-coverage-istanbul-reporter@1.3.0
├── karma-jasmine@1.1.0
├── karma-jasmine-html-reporter@0.2.2
├── protractor@5.1.2
├── rxjs@5.5.2
├── ts-node@3.0.6
├── tslint@5.3.2
├── typescript@2.3.4
├── web-animations-js@2.3.1
└── zone.js@0.8.12

Just in case somebody have similar problem as mine. 万一有人遇到与我类似的问题。

The problem was related to importing in some of the *.ts files. 该问题与某些* .ts文件的导入有关。 For example if You are importing some object like that: 例如,如果要导入这样的对象:

import { Observable } from 'rxjs';

It is wrong because then Visual Studio Code will not inform you about missing operators. 这是错误的,因为那样Visual Studio Code将不会通知您有关缺少运算符的信息。 The same with ng serve . ng serve

If you import like this: 如果您这样导入:

import { Observable } from 'rxjs/Observable';

Then Visual Studio Code and ng serve will inform You about missing operators. 然后, Visual Studio Codeng serve将通知您有关缺少的运算符的信息。 So be aware if You're importing from 'rxjs' directly. 因此,请注意您是否直接from 'rxjs'导入。 It is better to refactor all your code that contains imports from 'rxjs' to something like from 'rxjs/Observable' etc... 最好将包含from 'rxjs'导入的所有代码重构from 'rxjs/Observable'等的代码...

I get typescript reference checking with rxjs added operators. 我使用rxjs添加的运算符进行打字稿参考检查。

Try setting "typescript.referencesCodeLens.enabled": true (File/Preferences/Settings). 尝试设置"typescript.referencesCodeLens.enabled": true (文件/首选项/设置)。

A couple of other settings that may be relevent 其他一些可能是相关的设置

"typescript.validate.enable": true (with this off, rxjs reference errors aren't shown)
"javascript.validate.enable": true

Ref Extensions using CodeLens 使用CodeLens的参考扩展

You can see all errors in the Problems tab (Ctrl+Shift+M). 您可以在“问题”选项卡(Ctrl + Shift + M)中查看所有错误。


Another trick is to have all your rxjs imports in a top level file and import it into app.module. 另一个技巧是将所有rxjs导入到顶级文件中,然后将其导入到app.module中。

import './rxjs-extensions';

rxjs-extensions.js rxjs-extensions.js

import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Subscription } from 'rxjs/Subscription';

import 'rxjs/add/operator/defaultIfEmpty';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/concat';
...

import 'rxjs/add/observable/interval';
import 'rxjs/add/observable/empty';
import 'rxjs/add/observable/from';
import 'rxjs/add/observable/of';
import 'rxjs/add/observable/throw';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM