javascript - Angular 7 - 从导入的模块中获取组件
问题描述
我阅读了堆栈溢出向我展示的所有提出的问题,但都没有解决我的问题。
我是 Angular 的新手,我有几个Modules
和Components
. 我想要一个子模块来导入一个组件。然后让父模块导入子模块并创建它的组件。是的,我已经在子模块中导出了组件。
我想要这样:
- 父模块
- 子模块
- 组件(玩具)
- 子模块
但我得到了错误:
parent/parent.module.ts(8,5) 中的错误:错误 TS2304:找不到名称“ToyComponent”。
我不明白为什么这不起作用。我将不胜感激一些示例代码的解释。谢谢!
这是相关代码:
玩具组件
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-toy',
templateUrl: './toy.component.html',
styleUrls: ['./toy.component.css']
})
export class ToyComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
玩具组件 HTML
<p>
Awesome toy!
</p>
子模块
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ToyComponent } from './toy/toy.component';
import { ChildComponent } from './child.component';
@NgModule({
declarations: [
ToyComponent,
ChildComponent
],
imports: [
CommonModule
],
exports: [
ToyComponent
]
})
export class ChildModule { }
父模块
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ChildModule } from './child/child.module';
import { ParentComponent } from './parent.component';
@NgModule({
declarations: [
ToyComponent,
ParentComponent
],
imports: [
ChildModule,
CommonModule
],
exports: [
]
})
export class ParentModule { }
父组件 HTML
<p>
parent works! Child: Toy: <app-toy></app-toy>
</p>
解决方案
修复:
1.ToyComponent
从ParentModule
.
2. 声明ToyComponent
并Child Module
导出。
3.ChildModule
作为导入添加到您的父模块。
原因:
YourToyComponent
是在 your 下声明的ChildModule
。由于您ToyComponent
在您的中使用ParentModule
,您需要ToyComponent
从您的ChildModule
. 并ChildModule
在您的ParentModule
.
推荐阅读
- gnuplot - 迭代地在绘图/图形/画布上堆叠曲线
- c# - 如何将 Json 对象的 Json 数组转换为 XML C#
- c - 我将如何读取 C (*not* C++) 中的 NTFS 主文件表?
- soap - DocuSign SOAP 错误:此帐户缺少足够的权限
- java - ResponseStatusException 类的错误处理
- java - 如何自定义我的 log4j2.xml 参数
- python - 有没有办法在 python 中发送/按下 NUMPAD 键?
- c# - 递归方法的 IEnumerable 比使用 foreach 构造的相同 IEnumerable 慢 10 倍
- sql-server - SQL Server 2017 触发器 - 声明变量
- android - 在运行时验证 android 库的 (aar) 签名