gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:22:59 26 4
gpt4 key购买 nike

在将 angular 更新为 rc5 之后,我遇到了一个小问题。

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

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

现在我这样做了,但没有奏效......

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

还是我做错了什么??

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

最佳答案

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

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

NgModule documentation必读。

关于angular - [angular2][ngModules] 如何从其他模块调用组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39189673/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com