我有几个要包含在模块中的类,因此我可以只导入模块,因为它是一个不同的包,并从中使用这些类。这是一个小例子:
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