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

Angular 14项目编译报错求助:TS2585与NG6001错误

Angular 14项目添加fileUpload.ts后编译失败的解决方案

处理TS2585错误

  • 打开项目根目录的tsconfig.json
  • 找到compilerOptions下的lib配置,将其修改为包含es2015或更高版本,示例:
"compilerOptions": {
  "lib": ["es2015", "dom"]
}
  • 保存文件后重新执行编译

处理NG6001错误

根据FileUpload类的实际用途选择对应方案:

如果是普通工具类/服务

  • 打开app.module.ts,从declarations数组中删除FileUpload
  • 若为服务,可在类上添加@Injectable({ providedIn: 'root' })装饰器,无需在模块中手动声明;也可在模块的providers数组中添加该类

如果是组件/指令/管道

  • 在fileUpload.ts的类上方添加对应Angular装饰器:
    • 组件:
    import { Component } from '@angular/core';
    
    @Component({
      selector: 'app-file-upload',
      templateUrl: './file-upload.component.html',
      styleUrls: ['./file-upload.component.css']
    })
    export class FileUpload {
      // 类内容
    }
    
    • 指令:
    import { Directive } from '@angular/core';
    
    @Directive({
      selector: '[appFileUpload]'
    })
    export class FileUpload {
      // 类内容
    }
    
    • 管道:
    import { Pipe, PipeTransform } from '@angular/core';
    
    @Pipe({
      name: 'fileUpload'
    })
    export class FileUpload implements PipeTransform {
      transform(value: any): any {
        // 管道逻辑
      }
    }
    
  • 确保装饰器的导入语句正确无误

内容的提问来源于stack exchange,提问作者Bruno.mota

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 08:15:52