【问题标题】:[angular2][ngModules] how to call component from other module?[angular2][ngModules] 如何从其他模块调用组件?
【发布时间】:2019-01-23 15:46:58
【问题描述】:

将 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 {

}

应用组件:

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 卡)吗?在应用组件中?

还是我做错了什么??

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

【问题讨论】:

    标签: angular


    【解决方案1】:

    从 SimCardsModule 中导出 SimCardsComponent。只有导出的组件才能在导入模块中使用。

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

    NgModule documentation 是必读的。

    【讨论】:

    • 谢谢哥们!!我已经阅读了文档,在添加问题之前......我不匹配这部分!
    猜你喜欢
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    相关资源
    最近更新 更多