【问题标题】:Angular 2 : export component on a module and import and use it inside a moduleAngular 2:在模块上导出组件并在模块内导入和使用它
【发布时间】:2017-06-17 02:08:19
【问题描述】:

我有一个名为 CustomerInfoModule 的功能模块,它导出一个 CustomerInfoComponent。见下文。

import {NgModule} from '@angular/core'
import {RouterModule}  from '@angular/router'

import {CustomerInfoComponent} from './customer-info.component'

@NgModule({
declarations:[CustomerInfoComponent],
exports:[CustomerInfoComponent]
})
export class CustomerInfoModule{
}

我想在 MissedCollectionsComponent 中导入和使用这个 CustomerInfoComponent。我收到打字稿错误

'.module"' 没有导出的成员 'CustomerInfoComponent'

.

import {NgModule} from '@angular/core'
import {RouterModule} from '@angular/router'

import {MissedCollectionsComponent} from './missed-collections.component'

import {CustomerInfoComponent} from '../shared/customer/customer-info.module'

@NgModule({
imports:[RouterModule.forChild([
        {path:'missedcollection',component:MissedCollectionsComponent},
        {path:'missedcollection/customerinfo',component:CustomerInfoComponent}
    ]),
    CustomerInfoModule],
declarations:[],
exports:[]
})
export class MissedCollectionsModule{

}

根据 Angular2 文档,它说:

'我们导出 ContactComponent 以便其他模块导入 ContactModule 可以将它包含在他们的组件模板中。 link

从一个模块导入组件并在另一个模块中使用它是否不合逻辑。我是不是想错了/或错过了什么?

【问题讨论】:

    标签: angular typescript module components


    【解决方案1】:

    因为你是从模块文件导入的,你可以做这样的事情。

    客户信息.module.ts

    import {CustomerInfoComponent} from './customer-info.component';
    export {CustomerInfoComponent};
    

    【讨论】:

    • 可以的。谢谢。但我担心的是,当我们导出一个组件时,为什么从另一个模块导入它是不可见的。
    • @PrabashB 请注意,NgModule 中的exports 的意思是,如果您已经 imports 其他 NgModule declarations 它们,则不需要 declarations 一些组件、指令、管道。
    • 谢谢。有效。对您的代码的小修正。下面的代码对我有用。 import {CustomerInfoComponent} from './customer-info.component' export {CustomerInfoComponent} 请编辑您的代码。那么我会接受它。
    • 我想让我的 CustomerInfoComponent 封装在 CustomerInfoModule 中。因此,这里不接受直接导入文件组件的第二种选择。
    • 这里有一个更完整的答案会更好。提供没有真正解释的两行代码 sn-p 并不是特别有用。
    猜你喜欢
    • 2020-08-19
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 2020-08-27
    • 1970-01-01
    相关资源
    最近更新 更多