簡體   English   中英

VS 代碼 - Angular:對裝飾器的實驗性支持警告

[英]VS Code - Angular: Experimental support for decorators Warning

在 Angular (v10) 項目中創建一個空白文件 (sync.service.ts) 並添加以下代碼:

import { Injectable } from '@angular/core';

@Injectable({
    providedIn: 'root'
})
export class SyncService {

}

給出這個警告:

Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option in your 'tsconfig' or 'jsconfig' to remove this warning.

我打開了一個隨機文件(tag-cloud.component.ts)並添加了一個導入語句:

import { SyncService } from '../../services/sync.service';

警告消失。 但是警告消息根本不應該出現,因為:

@Injectable({
    providedIn: 'root'
})

請看下面的gif。 這是一個錯誤嗎?

在此處輸入圖像描述

Version: 1.47.1
Commit: 485c41f9460bdb830c4da12c102daff275415b53
Date: 2020-07-14T00:13:57.513Z
Electron: 7.3.2
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0
OS: Linux x64 5.4.0-40-generic

這是我在使用 WebStorm 時也遇到的一種奇怪行為。 我只能說,過了一會兒(無法告訴您是什么觸發了 IDE 正確理解),警告消失,一切恢復正常。

出現“問題”的原因是 Typescript 3.9 和 Angular 10 附帶的 tsconfig 文件(解決方案樣式 tsconfig)發生了變化。

更多信息在這里:

https://github.com/microsoft/TypeScript/issues/39632

https://docs.google.com/document/d/1eB6cGCG_2ircfS5GzpDC9dBgikeYYcMxghVH5sDESHw/edit#

Angular 10 中的解決方案樣式 tsconfig 影響

在 TypeScript 3.9 中,引入了解決方案樣式 tsconfig 的概念,在 Angular 中,我們看到了修復長期懸而未決的問題的能力和可能性,例如組件文件中的測試類型錯誤。 在 Angular CLI 版本 10 中,我們采用了腳手架新工作區和遷移現有工作區,以使用解決方案式 Typescript 配置。

當有一個未引用的文件時,這不會是任何 TypeScript 程序的一部分,這是因為 Angular CLI 使用文件選項而不是包含和排除來構建 tsconfig。

  1. 未引用的文件不繼承編譯器選項

    當有一個未引用的文件時,這將不是任何 TypeScript 程序的一部分,並且語言服務將回退以使用推斷的項目配置。

    下面是應用程序 tsconfig 文件 (tsconfig.app.json) 中所需更改的示例。 還需要更改其他 tsconfig 文件。

當前 tsconfig.app.json

 { "extends": "./tsconfig.base.json", "compilerOptions": {... }, "files": [ "src/main.ts", "src/polyfills.ts" ], "include": [ "src/**/*.d.ts" ] }

建議 tsconfig.app.json

 { "extends": "./tsconfig.base.json", "compilerOptions": {... }, "include": [ "src/**/*.ts" ], "exclude": [ "src/test.ts", "src/**/*.spec.ts", "src/**/*.server*", "src/**/*.worker.ts" ] }

A comment at the end of the first post in @Wolf359's answer led me to a simple workaround, in the event that your issue is cause by the tsconfig.json being split into tsconfig.base.json and tsconfig.app.json : add a tsconfig.json僅由{ "extends": "./tsconfig.base.json" }組成(我也對他的鏈接發表了同樣的評論)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM