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

Angular 15组件类报错(-992007):需添加显式Angular装饰器

Angular 15 组件报错:Class is using Angular features but is not decorated 解决方案

针对所有组件类均报错 Class is using Angular features but is not decorated. Please add an explicit Angular decorator.(-992007) 的问题,结合你的环境版本(Angular 15.1.13、Node 18.14.2、npm 9.5.0、Typescript 4.9.4),可以尝试以下解决方案:

  • 检查TypeScript配置文件(tsconfig.json)
    确保compilerOptions中启用装饰器相关特性,添加或修改以下配置:

    {
      "compilerOptions": {
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        // 其他已有配置...
      }
    }
    
  • 验证Angular项目配置(angular.json)
    确认architect > build > options > tsConfig指向正确的配置文件(通常是tsconfig.app.json),避免因路径错误导致编译器无法读取装饰器设置。

  • 检查组件文件的导入语句
    确保@Component和OnInit从@angular/core正确导入,避免拼写或路径错误:

    import { Component, OnInit } from '@angular/core';
    
  • 清理缓存并重新构建项目
    执行以下命令清理依赖缓存并重启开发服务:

    npm cache clean --force
    rm -rf node_modules
    rm package-lock.json
    npm install
    ng serve
    
  • 同步编辑器与项目的TypeScript版本
    如果使用VS Code等编辑器,确保其使用项目本地的TypeScript 4.9.4版本,而非全局版本。可通过编辑器右下角的TS版本选择器切换为Use Workspace Version。

内容的提问来源于stack exchange,提问作者Shraddha

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 20:12:11