[英]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:
我尝试了以下方法:
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.