我有一个应用程序,其中有许多简单的“模型”类,这些类在很多地方使用。我最终进行了许多导入,从各个文件导入所有这些。
在一个测试应用程序中(它是 Ionic4/Angular,但对于任何 Angular 应用程序都应该相同),我尝试将几个类包装到一个模块中,这样我就可以通过一个 import 语句来使用。
每个模型类只是一个普通类,例如
export class Model1 {
public name:string;
}
在
modules.module.ts
中,我有以下内容...
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Model1 } from './model1';
import { Model2 } from './model2';
@NgModule({
declarations: [],
imports: [
CommonModule
],
exports:[Model1, Model2]
})
export class ModelsModule { }
我现在希望在
HomeModule
中食用这个
在
home.module.ts
中我导入了模型模块..
@NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
ModelsModule, <------ my import
RouterModule.forChild([
{
path: '',
component: HomePage
}
])
],
declarations: [HomePage]
})
export class HomePageModule {}
到目前为止一切看起来都很好。
现在我想使用
home.page.ts
.. 中的模型
我添加了导入..
import { Model1, Model2 } from '../models/models/models.module';
然而IDE(VSCode)现在显示以下错误..
Module '"../models/models/models.module"' has no exported member 'Model1'.ts(2305) import Model1
我不知道我做错了什么。
我是否能够(并且推荐)使用
Modules
是这样的方式,还是有更好的方法来做我想做的事?
这里你犯了一个错误,你不能导出接口/类。您只能导出:
Other modules
Components
Directives
Pipes
您应该将模型作为单独的文件夹而不是在模块中。
NgModule
是一个角度概念,不应与打字稿模块混淆。