[angular2] [ngModules]如何从其他模块调用组件?

时间:2016-08-28 09:12:39

标签: angular

更新角度到rc5之后我有一个小问题。

我开始将我的应用程序重新编写为模块[ngModules]。

我有一个小问题,我有两个不同的模块,但module1需要从其他模块调用一个指令(组件)。

现在我这样做,但没有工作......

AppModule(模块1):

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';

import { HttpModule }     from '@angular/http';

import { SimCardsModule } from './+sim-cards/sim-cards.module';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    CommonModule,
    FormsModule,
    HttpModule,
    SimCardsModule
  ],
  providers: [],
  entryComponents: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule {

}

AppComponent:

import { Component } from '@angular/core';

declare let $: any;

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css']
})
export class AppComponent {

}

并在模板中

<sim-cards> loading </sim-cards>

现在我有sim-cards.module(模块2):

import { NgModule, ApplicationRef } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

import { HttpModule }     from '@angular/http';

import { SimCardsComponent } from './sim-cards.component';
import { SimCardsService } from './sim-cards.service';

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

        CommonModule,
        FormsModule,
        HttpModule
    ],
    providers: [SimCardsService],
    entryComponents: [SimCardsComponent],
    bootstrap: [SimCardsComponent]
})
export class SimCardsModule {

}

和sim-cards.component(模块2):

import {Component, OnInit, ViewEncapsulation} from '@angular/core';
import {SimCardsService} from './sim-cards.service';
import {IfEmptySetDefaultPipe} from '../pipes/if-empty-set-default.pipe';
import {IsInternalSimCardPipe} from '../pipes/is-internal-sim-card.pipe';
import {ClientNumberSimCardPipe} from '../pipes/client-number-sim-card.pipe';
import {OperatorIdSimCardPipe} from '../pipes/operator-id-sim-card.pipe';

declare let $: any;

@Component({
    selector: 'sim-cards',
    templateUrl: 'sim-cards.component.html',
    styleUrls: ['sim-cards.component.scss'],
    encapsulation: ViewEncapsulation.None,
    pipes: [IfEmptySetDefaultPipe, IsInternalSimCardPipe, ClientNumberSimCardPipe, OperatorIdSimCardPipe]
})
export class SimCardsComponent implements OnInit {
...
}

如何以正确的方式做到这一点?我是否需要在appmodule中导入组件(sim卡)?在appcomponent?

或者我做错了什么?

在浏览器中我只得到字符串,加载......在控制台中没有错误。

1 个答案:

答案 0 :(得分:12)

从SimCardsModule导出SimCardsComponent。只有导出的组件才可用于导入模块。

@NgModule({
    exports: [SimCardsComponent],
    ...
})
export class SimCardsModule {

https://www.doulos.com/knowhow/sysverilog/tutorial/interfaces/是必读的。