避免组件文件顶部模型的导入语句的解决方案



我使用Angular已经有一段时间了。我觉得我的模块化缺乏一些心血来潮。我花了一些时间研究NgModule和它的属性,我有信心尝试升级我的应用的模块化,但它并没有像我预期的那样工作。让我恼火的第一件事是,我在每个组件的文件示例

中都导入了几个模型。ts组件

import {Object, Tool, Color, SearchOption ...etc}  from '..someplace';

我试图创建一个模块来包含我所有的模型

@NgModule(
{
provider: [Object, Tool, Color, SearchOptions]
}
)
export class ModelModule{
}

然后我把这个模块导入到一个特性的模块中。

@NgModule({
declarations: [],
imports: [
CommonModule,

ModelModule
],

})
export class ResultSearch { }

但是我的Component类抱怨缺少类型这是与导入"模型模块"的模块相关的组件。对我来说,导入模型模块将允许我避免导入。

@Component({
select: 'someselector'
})
export class Search{
tool: Tool  <<--- cannot find name 'Tool'
}

我将给出我的结构的概念。

/model
--model.module.ts <<-- all the models are include here
--tool.model.ts
--color.model.ts
/result
--result.module.ts    <<-- I import the model module here
--result.component.ts  <<-- My desire was to avoid the imports here on the component

ES模块格式是打包JavaScript代码和Typescript代码的官方标准格式。如果在单独的文件中使用接口/类定义,则只需导入其符号。没什么问题。

最新更新