简体   繁体   English

Angular 11 '错误 NG2007:Class 正在使用 Angular 功能但未修饰。 但是 class 是装饰的

[英]Angular 11 'error NG2007: Class is using Angular features but is not decorated.' But the class is Decorated

In my angular workspace I have several libraries and 2 apps.在我的 angular 工作区中,我有几个库和 2 个应用程序。

I have a Component class "LogoutComponent" in my library.我的库中有一个组件 class“LogoutComponent”。 I compiled it using ng build mms-common --watch.我使用 ng build mms-common --watch 编译了它。 I have no errors.我没有错误。

The app I am currently working on is called "my" When I use ng serve my, I get an我目前正在使用的应用程序称为“我的”当我使用 ng serve my 时,我得到一个

Error: dist/mms-common/fesm2015/mms-common.js:99:7 - error NG2007: Class is using Angular features but is not decorated.错误:dist/mms-common/fesm2015/mms-common.js:99:7 - 错误 NG2007: Class 正在使用 Angular 功能但未修饰。 Please add an explicit Angular decorator.请添加一个明确的 Angular 装饰器。

99 class LogoutComponent ~~~~~~~~~~~~~~~ 99 class 注销组件 ~~~~~~~~~~~~~~~

But I have the component decorated.但是我装饰了组件。 Not only that but I am not using the Logout Component at this time.不仅如此,我现在还没有使用注销组件。 I have tried the following:我尝试了以下方法:

  • deleted the dist/mms-common directory删除了 dist/mms-common 目录
  • compiled the mms-common directory using --prod flag使用 --prod 标志编译 mms-common 目录
  • deleted the dist/my directory删除了 dist/my 目录
  • deleted and recreated the LogoutComponent删除并重新创建了 LogoutComponent
  • removing all of the code from the LogoutComponent excluding the decorator and basic class call从 LogoutComponent 中删除除装饰器和基本 class 调用之外的所有代码
  • compiled the my app with and without --prod flag使用和不使用 --prod 标志编译我的应用程序
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(['']);
  }

} 

My library module is我的库模块是

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 { }

my public-api is我的公共 api 是

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

I had a similar situation when running ng serve .我在运行ng serve时遇到了类似的情况。 I was getting that error for 3rd party libraries and for all my Angular components even though they were decorated.对于第 3 方库和我的所有 Angular 组件,即使它们已被装饰,我也会收到该错误。

The problem for me was that I was targeting es6 .对我来说问题是我的目标是es6 I changed my tsconfig.json to target es5 :我将tsconfig.json更改为目标es5

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

So the answer was in the "my" project.所以答案就在“我的”项目中。 I had created a component and was using one of the services exported by the mms-common library.我创建了一个组件并使用了 mms-common 库导出的服务之一。 But the import statement on the new component was:但是关于新组件的导入声明是:

import { UserService } from 'dist/mms-common/fesm2015/mms-common';

The proper form of the import should have been正确的导入形式应该是

import { UserService } from 'mms-common';

The problem itself was created by visual studio code auto complete.问题本身是由 Visual Studio 代码自动完成造成的。

This does solve the problem but why did the compiler did not point to the new component.这确实解决了问题,但为什么编译器没有指向新组件。

暂无
暂无

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

相关问题 问:Angular 组件损坏 - “类正在使用 Angular 功能但未装饰。请添加显式 Angular 装饰器” - Q: Angular components broken - "Class is using Angular features but is not decorated. Please add an explicit Angular decorator" 类使用 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 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 “......用可注射装饰” - Angular2 - "...decorated with Injectable" - Angular2 如果装饰了角度2组件,它们的类型是什么? - What is the type of angular 2 components if they are decorated? Angular2中的装饰/计算数组 - Decorated/Computed arrays in Angular2 Angular 2 ng-class 在 IE 11 中不起作用 - Angular 2 ng-class not working in IE 11 Angular 4:获取所有用@Input 修饰的属性 - Angular 4: Get all properties that are decorated with @Input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM