简体   繁体   English

问:Angular 组件损坏 - “类正在使用 Angular 功能但未装饰。请添加显式 Angular 装饰器”

[英]Q: Angular components broken - "Class is using Angular features but is not decorated. Please add an explicit Angular decorator"

After many hours of trying to find a solution, I have come up short.经过数小时的努力寻找解决方案,我做不到了。 Without fail, every time I generate a new component in my Angular app it is the same message.毫无疑问,每次我在我的 Angular 应用程序中生成一个新组件时,它都是相同的消息。 "Class is using Angular features but is not decorated. Please add an explicit Angular decorator" “类正在使用 Angular 功能但未装饰。请添加显式 Angular 装饰器”

Below are some of my components and their decorators.下面是我的一些组件和它们的装饰器。

组件 1

在此处输入图像描述

And even a test component fresh out of the CLI for good measure.甚至是从 CLI 中新鲜出来的测试组件也可以很好地衡量。

在此处输入图像描述

I know I can get rid of the problem by removing OnInit implementation.我知道我可以通过删除 OnInit 实现来解决这个问题。 But in other components I do need it.但在其他组件中,我确实需要它。

Things I have tried我尝试过的事情

delete node_modules, reinstall and downgrade and upgrade TypeScript version from 4.7.2 to 4.8.2 then to 4.7.2 again.删除 node_modules,重新安装并降级并将 TypeScript 版本从 4.7.2 升级到 4.8.2,然后再次升级到 4.7.2。

TS Config TS 配置

/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": false,
    "noImplicitOverride": true,
    "noPropertyAccessFromIndexSignature": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2020",
    "module": "es2020",
    "lib": [
      "es2020",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "enableI18nLegacyMessageIdFormat": false,
    "strictInjectionParameters": true,
    "strictInputAccessModifiers": true,
    "strictTemplates": true
  }
}

Package.json Package.json

"dependencies": {
    "@angular/animations": "^14.1.0",
    "@angular/cdk": "^14.2.0",
    "@angular/common": "^14.1.0",
    "@angular/compiler": "^14.1.0",
    "@angular/core": "^14.1.0",
    "@angular/forms": "^14.1.0",
    "@angular/platform-browser": "^14.1.0",
    "@angular/platform-browser-dynamic": "^14.1.0",
    "@angular/router": "^14.1.0",
    "daisyui": "^2.24.0",
    "qs": "^6.11.0",
    "rxjs": "~7.5.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^14.1.2",
    "@angular/cli": "~14.1.2",
    "@angular/compiler-cli": "^14.1.0",
    "@tailwindcss/forms": "^0.5.2",
    "@types/jasmine": "~4.0.0",
    "autoprefixer": "^10.4.8",
    "jasmine-core": "~4.2.0",
    "karma": "~6.4.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.2.0",
    "karma-jasmine": "~5.1.0",
    "karma-jasmine-html-reporter": "~2.0.0",
    "postcss": "^8.4.16",
    "tailwindcss": "^3.1.8",
    "typescript": "^4.7.2"
  }

I also appear to have lost auto-complete for adding components in the html file.我似乎也失去了在 html 文件中添加组件的自动完成功能。 Eg in app module - no dropdown after typing <app-...>.例如在应用程序模块中 - 键入 <app-...> 后没有下拉菜单。 Any suggestions?有什么建议么?

Hopefully this helps希望这会有所帮助

You'll need to add a @Component decorator to that base class (which should probably also be declared abstract).您需要将@Component装饰器添加到该基础 class (可能也应该声明为抽象)。

This is the bare minimum you can get away with in Angular 9:这是您在 Angular 9 中可以摆脱的最低限度:

@Component({
  template: ''
})
export abstract class ComponentName {

    @Input() teamName: string;
    @Input() teamSize: number;
    @Input() players: any;
}

For Angular 10+, see this answer对于 Angular 10+, 请参阅此答案

please check the solutions form the answer请从答案中检查解决方案

It was my folder structure (atleast I believe).这是我的文件夹结构(至少我相信)。

I had node modules of my server and ts config in the root of my folder, and then my client app as a folder in the root too.我的服务器的节点模块和 ts 配置在我的文件夹的根目录中,然后我的客户端应用程序也作为根目录中的文件夹。

The solution was to move EVERYTHING related to server into its own folder, no node modules or ts config.解决方案是将与服务器相关的所有内容移动到自己的文件夹中,没有节点模块或 ts 配置。

reponame, client and server folders. reponame、客户端和服务器文件夹。 not client and server folders, with server tsconfig and node modules in root.不是客户端和服务器文件夹,服务器 tsconfig 和节点模块位于根目录中。

暂无
暂无

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

相关问题 Angular 15: Class 正在使用 Angular 功能但未修饰。 请添加明确的 Angular 装饰器。(-992007) - Angular 15 : Class is using Angular features but is not decorated. Please add an explicit Angular decorator.(-992007) 类使用 Angular 特性但没有修饰。 请添加一个显式的 Angular 装饰器 - Class is using Angular features but is not decorated. Please add an explicit Angular decorator 如何在不出错的情况下实现 OnInit:Class 正在使用 Angular 功能但未进行装饰。 请添加显式 Angular 装饰器 - How do I implement OnInit without getting error: Class is using Angular features but is not decorated. Please add an explicit Angular decorator Angular 11 '错误 NG2007:Class 正在使用 Angular 功能但未修饰。 但是 class 是装饰的 - Angular 11 'error NG2007: Class is using Angular features but is not decorated.' But the class is Decorated Class 正在使用 Angular 功能,但未在 angular 14 中进行修饰 - Class is using Angular features but is not decorated in angular 14 Class 正在使用 Angular 功能但未使用 Typescript 4.8.2 进行装饰 - Class is using Angular features but is not decorated with Typescript 4.8.2 Angular 库基础 class 没有任何装饰器但使用 Angular 功能 - Angular Library base class without any decorator but using Angular features 如果装饰了角度2组件,它们的类型是什么? - What is the type of angular 2 components if they are decorated? 如何使用 typescript 中的装饰器向 class 添加和声明新属性(角度) - How to add and declare new property to class using decorator in typescript (angular) 装饰器何时以及如何应用于@angular包中的装饰类 - When and how s decorator applied to the decorated classes from the @angular packages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM