angular - 错误未捕获的错误:在应用程序模块中声明时模板解析错误
问题描述
不知道发生了什么以及为什么当我更改上传位置时会弹出此错误。本质上,我正在将一个组件从只能在一个组件中访问到整个应用程序,现在我得到了那个错误。
app.module.ts
import { DefaultProfilePictureComponent } from '../sharedModules/components/default-profile-picture/default-profile-picture.component'
@NgModule({
declarations: [
...
DefaultProfilePictureComponent,
...
],
...
})
默认配置文件图片.component.ts
@Component({
selector: 'app-default-profile-picture',
templateUrl: './default-profile-picture.component.html',
styleUrls: ['./default-profile-picture.component.scss'],
})
export class DefaultProfilePictureComponent implements OnInit {
@Input() private lastNames: string;
@Input() private big: boolean;
@Input() private contactBox: boolean;
private char1: string;
private char2?: string;
public constructor() {}
public ngOnInit(): void {
console.log(this.lastNames.split(' ').length);
if (this.lastNames.split('').length > 1) {
this.char1 = this.lastNames.split(' ')[0][0].toUpperCase();
this.char2 = this.lastNames.split(' ')[1][0].toUpperCase();
} else {
this.char1 = this.lastNames.split(' ')[0][0].toUpperCase();
}
}
}
默认配置文件图片.component.html
<div class="profile-container" *ngIf="big">
<div class="profileImageBig">{{ char1 }} {{ char2 || '' }}</div>
</div>
主页.html
<app-default-profile-picture
*ngIf="!person.profilePicture"
lastNames="person.lastNames"
contactBox="true"
></app-default-profile-picture>
在我尝试使用该组件的任何地方,我都会收到错误消息
app-default-profile-picture' 不是已知元素:
但是如果我将它包含在中,home.page.module.ts
那么我会收到错误消息,指出它是在两个模块中声明的。是什么赋予了?谢谢!
解决方案
问题是您使用错误。组件只能在您导入它的模块中使用。在您的情况下,问题是您无法加载由另一个模块中的模块导入的组件以允许它use.export 来自 home.page.module.ts 的组件
@NgModule({
declarations: [
...
DefaultProfilePictureComponent,
...
],
exports:[ DefaultProfilePictureComponent]
...
})
并将其导入您想要的其他模块中,例如 app.modules.ts
app.module.ts
@NgModule({
imports:[
BrwsrModule,
...,
HomePageModule
],
...
})
推荐阅读
- python - 在单独的行中列出理解输出
- python-3.x - 使用flask-restplus时,如何编组一个简单列表(没有属性)的json?
- matlab - 如何编辑或剪切 Y 轴限制?
- c# - 如何以 xamarin 形式将列表保存到 sqlite 数据库中
- sql - 从 SQL Server 检索任何外部表数据时,Postgresql 外部数据包装器引发错误
- photoshop - 用于调整大小的 Photoshop 脚本(输出 = png,最近邻兼容性)?
- python - 使用 Python 和 BeautifulSoup 开始 Web Scraping - 分步教程中的错误
- java - 有没有办法使用“加入”查询从 Firebase 数据库中检索 android 中的数据?
- mongodb - 如何从过去一小时内没有记录的 Mongo 获取所有 ID
- python - 为什么我对两个熊猫系列做**操作时结果不是缺失值?