angular - 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~~~~~~~~~~~~~~~
但是我装饰了组件。不仅如此,我目前还没有使用注销组件。我尝试了以下方法:
- 删除了 dist/mms-common 目录
- 使用 --prod 标志编译 mms-common 目录
- 删除 dist/my 目录
- 删除并重新创建了 LogoutComponent
- 从 LogoutComponent 中删除所有代码,不包括装饰器和基本类调用
- 在有和没有 --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(['']);
}
}
我的图书馆模块是
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'
解决方案
我跑步的时候也遇到过类似的情况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
...
},
...
}
推荐阅读
- java - 使特殊字符留在索引java中
- c# - 自动化测试从设置中列出的代码中抛出错误,尽管它已经在其他测试中正确执行
- arrays - Swift Array of Ints - 查找与前一个元素的差异大于 10 的索引
- soap - 如何在不发送任何 SOAP 标头的情况下将消息发送到基于 SOAP 的 Web 服务
- c++ - Windows XP 上的 WinHttpSendRequest 和 HTTPS
- json - 如何输出列表以 vuetify 数据表?
- java - 不要如何解决火花集群中GC中的问题,有人可以解释如何处理ParOldGen和PSYoungGen吗?
- windows - 如果 exe 和目标 dll 是 32 位,为什么我会在 LoadLibrary 中收到错误 193?
- android - Firebase:加载本地数据
- javascript - 将二维数组变成一棵树