类正在使用 Angular 功能,但未以 Angular 14 进行装饰

Ljt*_*Ljt 14 typescript angular

我已将我的 Angular 应用程序从 13 更新到 14。更新后,在 vs code 中打开组件类后出现以下错误。

类正在使用 Angular 功能,但未进行修饰。请添加显式的 Angular 装饰器

我已经检查过这个问题

但我没有在应用程序中使用任何继承性。

在此输入图像描述

但应用程序运行完美。如何消除该错误?

ts.confing

/* 要了解有关此文件的更多信息,请参阅: https: //angular.io/config/tsconfig。*/

{
  "compileOnSave": false,
  "compilerOptions": {
    
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": false,
    "strict": false,
    "allowSyntheticDefaultImports":true, 
    "noImplicitOverride": true,
    "noPropertyAccessFromIndexSignature": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "sourceMap": true,
    "declaration": true,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2022",
    "module": "es2022",
    "lib": [
      "es2022",
      "dom"
    ],
    "paths": {
      "stream": [ "./node_modules/stream-browserify" ]
    },
  },
  "angularCompilerOptions": {
    "enableI18nLegacyMessageIdFormat": false,
    "strictInjectionParameters": true,
    "strictInputAccessModifiers": true,
    "strictTemplates": false,
  }
}
Run Code Online (Sandbox Code Playgroud)

验证组件

import { Component, Input } from '@angular/core';
import { AbstractControl } from '@angular/forms';
import { ValidationService } from 'src/app/core/services/validation.service';

@Component({
  selector: 'app-validation-message',
  template: '<div *ngIf="errorMessage !== null" style="width:50%;margin-top:.25rem;font-size:80%;color:#dc3545">{{errorMessage}}</div>'
})

export class ValidationMessageComponent {
  @Input() control: AbstractControl;

  constructor() {
  }

  get errorMessage() {
    if (this.control.errors) {
    
      for (let propertyName in this.control.errors) {
        if (this.control.errors.hasOwnProperty(propertyName) && this.control.touched) {
          return ValidationService.getValidatorErrorMessage(propertyName, this.control.errors[propertyName]);
        }
      }
    }
    return null;
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 23

我遇到了同样的问题,我通过降级打字稿依赖关系来解决它:

"@typescript-eslint/eslint-plugin": "5.29.0",
"@typescript-eslint/parser": "5.29.0",
"typescript": "4.7.2"
Run Code Online (Sandbox Code Playgroud)

现在错误消失了,一切正常

  • TS 降级后,您可能还需要重新启动 IDE (4认同)

Edm*_*nis 9

有同样的问题。Typescript 从 4.7.4 降级到 4.7.2 解决了该问题。

npm i typescript@4.7.2


Aks*_*Aks 7

这是由于 Angular Language Service 扩展,它还不支持 Typescript 4.8,请参阅此处的相关问题: https: //github.com/angular/vscode-ng-language-service/issues/1746

编辑:问题现已解决,扩展现已更新,因此不需要降级。