gpt4 book ai didi

angular - 通过模块导入多个 Angular 组件

转载 作者:太空狗 更新时间:2023-10-29 16:55:47 24 4
gpt4 key购买 nike

我的 Angular 项目正在增长,所以我想保持我的项目干净。

我有一个 Angular 组件,它依赖于一个嵌套的 Angular 组件。

现在我需要两个 import 语句来使用这些组件,这些组件没有彼此就无法工作。

作为解决方案,我想创建一个包含这两个组件的小型 Angular 模块,并将该模块导入到我的主 app.module 中。

执行此操作后,我收到一条错误消息,指出无法识别小模块内的组件之一。

//app.module.ts
@NgModule({
imports: [BrowserModule, HttpModule, DictaatModule],
declarations: [AppComponent, DictatenComponent, FilePreviewComponent],
bootstrap: [AppComponent]
})


//Dictaat.module.ts
import { DictaatComponent } from './dictaat.component';
import { DictaatEntryComponent } from './dictaat-entry.component';

@NgModule({
imports: [BrowserModule, HttpModule],
declarations: [DictaatComponent, DictaatEntryComponent],
})
export class DictaatModule{ }

我的问题:将多个组件组合到一个模块中是否是一种好的做法,Angular 是否已经支持这种做法?

附言。我正在使用 Angular 2.0.0,而不是任何 RC。我的设置与英雄之旅教程的设置相当。

编辑:源代码 https://github.com/Linksonder/Webdictaat/

错误信息:


Unhandled Promise rejection: Template parse errors:
Can't bind to 'dictaatName' since it isn't a known property of 'wd-dictaat'.
1. If 'wd-dictaat' is an Angular component and it has 'dictaatName' input, then verify that it is part of this module.
2. If 'wd-dictaat' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message.
("
</div>
<div class="col-md-3">
<wd-dictaat [ERROR ->][dictaatName]="selectedDictaat.name" *ngIf="selectedDictaat">Loading dictaat...</wd-dictaat>
</d"): DictatenComponent@21:20
'wd-dictaat' is not a known element:
1. If 'wd-dictaat' is an Angular component, then verify that it is part of this module.
2. If 'wd-dictaat' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message. ("
</div>
<div class="col-md-3">
[ERROR ->]<wd-dictaat [dictaatName]="selectedDictaat.name" *ngIf="selectedDictaat">Loading dictaat...</wd-dicta"): DictatenComponent@21:8 ; Zone: <root> ; Task: Promise.then ; Value: Error: Template parse errors:(…) Error: Template parse errors:
Can't bind to 'dictaatName' since it isn't a known property of 'wd-dictaat'.
1. If 'wd-dictaat' is an Angular component and it has 'dictaatName' input, then verify that it is part of this module.
2. If 'wd-dictaat' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message.
("
</div>
<div class="col-md-3">
<wd-dictaat [ERROR ->][dictaatName]="selectedDictaat.name" *ngIf="selectedDictaat">Loading dictaat...</wd-dictaat>
</d"): DictatenComponent@21:20
'wd-dictaat' is not a known element:
1. If 'wd-dictaat' is an Angular component, then verify that it is part of this module.
2. If 'wd-dictaat' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message. ("
</div>
<div class="col-md-3">
[ERROR ->]<wd-dictaat [dictaatName]="selectedDictaat.name" *ngIf="selectedDictaat">Loading dictaat...</wd-dicta"): DictatenComponent@21:8
at TemplateParser.parse (http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js:8530:21)
at RuntimeCompiler._compileTemplate (http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js:16905:53)
at eval (http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js:16828:85)
at Set.forEach (native)
at compile (http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js:16828:49)
at ZoneDelegate.invoke (http://localhost:3000/node_modules/zone.js/dist/zone.js:192:28)
at Zone.run (http://localhost:3000/node_modules/zone.js/dist/zone.js:85:43)
at http://localhost:3000/node_modules/zone.js/dist/zone.js:451:57
at ZoneDelegate.invokeTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:225:37)
at Zone.runTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:125:47)

最佳答案

您需要将您的组件添加到 Dictaat.module.ts 的导出中,以便将它们导入您的 app.module.ts:

//Dictaat.module.ts
import { DictaatComponent } from './dictaat.component';
import { DictaatEntryComponent } from './dictaat-entry.component';

@NgModule({
imports: [BrowserModule, HttpModule],
declarations: [DictaatComponent, DictaatEntryComponent],
exports: [DictaatComponent, DictaatEntryComponent]
})

export class DictaatModule{ }

关于angular - 通过模块导入多个 Angular 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39572278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com