【发布时间】:2017-09-09 01:23:50
【问题描述】:
我有几个类想要包含在一个模块中,因此我可以只导入该模块,因为它是一个不同的包,并从中使用这些类。这是一个小例子:
human.ts(我的类文件)
export class Human {
private numOfLegs: Number;
constructor() {
this.numOfLegs = 2;
}
}
test.module.ts(我的模块文件)
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Human } from './human';
@NgModule({
imports: [CommonModule],
declarations: [
Human
],
providers: [],
exports: [Human]
})
export class TestModule {}
如何在组件中实例化 Human 类? 我都试过了:
import { TestModule } from './test.module';
和
import { Human } from './test.module';
但如果我这样做new Human(),我仍然会得到cannot find name Human
【问题讨论】:
-
我认为您仍然必须在组件中导入 Human 才能使用它,就像您要使用 Http 或任何其他对象一样。
-
在我曾经为重复投票的线程中,已经说明了答案。顺便说一句,如果你将服务导入到 NgModule 中,你仍然需要将类导入到你将使用它的组件中。 javascript 导入和 NgModule 导入是两个完全不同的东西,可能会有点混乱。
-
那么我尝试做的事情错了吗?
-
类不能封装到模块中?
标签: angular typescript ng-modules