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

Angular中@Injectable装饰器签名解析失败及类型不匹配问题求助

Angular类装饰器签名解析错误解决办法

错误信息

调用类装饰器作为表达式时无法解析其签名。类型“ClassDecoratorContext”无法分配给类型“string | symbol | undefined”。

问题场景

在App Module下创建Book Module后,在该模块内编写服务文件时触发了上述错误。

相关代码

服务文件(book.service.ts)

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'

@Injectable({
  providedIn:"root"
})
export class BookService {

  constructor(private _http: HttpClient){ }
}

Book模块文件(book.module.ts)

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { BookRoutingModule } from './book-routing.module';
import { HomeComponent } from './home/home.component';
import { bookReducer } from './store/reducer/book.reducer';
import { StoreModule } from '@ngrx/store';


@NgModule({
  declarations: [
    HomeComponent
  ],
  imports: [
    CommonModule,
    BookRoutingModule,
    StoreModule.forFeature('bookReducer',bookReducer)
  ]
})
export class BookModule { }

文件结构

src/app目录下包含:

  • book目录:内有home组件文件夹、store目录、book-routing.module.ts、book.module.ts、book.service.ts
  • 根模块文件app.module.ts等核心文件

解决方法

  • 版本兼容性检查:确保TypeScript版本与当前Angular版本匹配。执行ng version查看版本对应关系,比如Angular 16+需要搭配TypeScript 5.0及以上版本,通过npm install typescript@<对应版本号> --save-dev完成更新。
  • tsconfig配置修正:打开tsconfig.json,确认compilerOptions中experimentalDecorators和emitDecoratorMetadata均设置为true。
  • 重置依赖:删除node_modules文件夹和package-lock.json文件,执行npm install重新安装所有依赖。
  • 编辑器缓存清理:重启当前代码编辑器,清除缓存后重新加载项目,避免语法检测异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 12:34:56