我对棱角很陌生,很难在另一个组件中显示一个组件。
模式:我有一个导航栏,使路由器出口使用路由导航到"component1“。然后,在组件1中,我将标记保持不变。如果我导航到组件-2使用路由,它正确地呈现。
main.component.html
<div class="flex">
<div>
<router-outlet name="navbar"></router-outlet>
<div class="container-fluid col-10 offset-2">
<div class="row">
<div class="col-12">
<div class="card jh-card shadow">
<router-outlet></router-outlet>
<router-outlet name="popup"></router-outlet>
</div>
<jhi-footer></jhi-footer>
</div>
</div>
</div>
</div>
</div>component1.module.ts
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { RouterModule } from '@angular/router';
import { ClientSharedModule } from '../shared';
import { COMPONENT1_ROUTE, Component1 } from './';
@NgModule({
imports: [
ClientSharedModule,
RouterModule.forRoot([ COMPONENT1_ROUTE ], { useHash: true }),
],
declarations: [
Component1
],
entryComponents: [
],
providers: [
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class Component1Module {}component1.component.html
<div class="row">
<div class="col-md-1"></div>
<div class="col-md-10">
<div class=""> searchBar</div>
<div> some text</div>
</div>
<div class="col-md-1"></div>
</div>
<component-2>test</component-2>component-2.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'component-2',
templateUrl: './component-2.component.html',
styles: [
'component-2.scss'
]
})
export class Component2 implements OnInit {
constructor() {
console.log('COMPONENT CONSTRUCTED');
}
ngOnInit() {
console.log('COMPONENT INITIALISED');
}
}custom-shared.module.ts
import {CUSTOM_ELEMENTS_SCHEMA, NgModule} from '@angular/core';
import {
Component2Module
} from './component-2/component-2.module';
@NgModule({
imports: [
Component2Module,
],
declarations: [
],
entryComponents: [],
exports: [
Component2Module
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class CustomSharedModule { }component-2.module.ts
import {NgModule, CUSTOM_ELEMENTS_SCHEMA} from '@angular/core';
import {RouterModule} from '@angular/router';
import {ClientSharedModule} from '../../shared';
import {
Component2Details,
Component2,
component2Route
} from './';
const ENTITY_STATE = [
component2Route,
];
@NgModule({
imports: [
ClientSharedModule,
RouterModule.forChild(ENTITY_STATE)
],
declarations: [
Component2,
Component2Details
],
entryComponents: [
Component2,
Component2Details
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class Component2Module {
constructor() {}
}我换了零件的真名。实际上,我在编译时和执行时都没有错误。我相信这与范围或注射有关,但我找不到解决办法。如果有人能给我一些帮助,我会非常感激的。
附加信息: Jhipster v4.14.2单片应用程序。角5+
提前谢谢。文森特湾
发布于 2018-04-18 07:58:36
问题解决了,我的错误是,如果我在compent2模块中导出component2,然后在我的共享模块中导入这个模块,那么在我导入共享模块的任何地方,组件都是可用的。
解决方案是直接从共享模块导出/声明component2。然后,无论我在哪里导入共享模块,它都是可用的。
希望这能帮到一些人。再见
https://stackoverflow.com/questions/49865505
复制相似问题