首页 > 解决方案 > Angular 11 '错误 NG2007:类正在使用 Angular 功能,但未装饰。' 但是班级是装饰的

问题描述

在我的角度工作区中,我有几个库和 2 个应用程序。

我的库中有一个组件类“LogoutComponent”。我使用 ng build mms-common --watch 编译它。我没有错误。

我目前正在开发的应用程序称为“我的”当我使用 ng serve my 时,我得到一个

错误:dist/mms-common/fesm2015/mms-common.js:99:7 - 错误 NG2007:类正在使用 Angular 功能但未装饰。请添加一个明确的 Angular 装饰器。

99类LogoutComponent~~~~~~~~~~~~~~~

但是我装饰了组件。不仅如此,我目前还没有使用注销组件。我尝试了以下方法:

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { User } from '../models/user';
import { UserService } from '../services/user.service';

@Component({
  selector: 'lib-logout',
  templateUrl: './logout.component.html',
  styleUrls: ['./logout.component.css']
})
export class LogoutComponent implements OnInit {

  constructor(
    private router: Router,
    private userService: UserService
  ) { }

  ngOnInit(): void {
    this.userService.logout();
    this.userService.currentUser = new User();
    this.router.navigate(['']);
  }

} 

我的图书馆模块是

import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { LogoutComponent } from './logout/logout.component';

@NgModule({
  declarations: [LogoutComponent],
  imports: [
    RouterModule
  ],
  exports: [LogoutComponent]
})
export class MMSCommonModule { }

我的公共 api 是

export * from './lib/models/user';
export * from './lib/services/user.service';
export * from './lib/mms-common.module';
export * from './lib/logout/logout.component'

标签: angular

解决方案


我跑步的时候也遇到过类似的情况ng serve。对于 3rd 方库和我的所有 Angular 组件,即使它们已被装饰,我也遇到了该错误。

对我来说,问题是我的目标是es6. 我改变了我tsconfig.json的目标es5

{
  "compileOnSave": true,
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "enableIvy": true,
  },
  "compilerOptions": {
    "lib": ["es6", "dom"],
    "module": "es6",
    "target": "es5", // <-- CHANGE THIS FROM es6 to es5
    ...
  },
  ...
}


推荐阅读