简体   繁体   中英

No component from another module is displayed

I tried display component with name main , using selector: 'app-main'. Component main is in NavbarModuleModule.

In NavbarModuleModule i make export main component.

I added NavbarModuleModule module to main module with name app.module .

Using selector I add him to main template but still component is not loaded.

在此处输入图片说明

navbar-module.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MainComponent } from './main/main.component'; 

@NgModule({
  declarations: [MainComponent],
  imports: [
    CommonModule 
  ],
  exports : [MainComponent] 
})
export class NavbarModuleModule { }

Main.component.html

<p>main works!</p> 

main.component.ts

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

@Component({
  selector: 'app-main',
  templateUrl: './main.component.html',
  styleUrls: ['./main.component.scss']
})
export class MainComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

app.module.ts - main module

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { HeaderComponent } from './header/header.component';
import { NavbarModuleModule } from './navbar-module/navbar-module.module';
import { HttpClientModule } from "@angular/common/http";    

@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent
  ],
  imports: [
    BrowserModule, 
    HttpClientModule,
    NavbarModuleModule
  ],
  providers: [],
  bootstrap: [AppComponent, HeaderComponent]
})

export class AppModule { }

Can anyone tell me what i'm doing wrong? How can I load this component with other module.

you need to add your maincomponent in bootstrap array which is in Appmodule.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { HeaderComponent } from './header/header.component';
import { NavbarModuleModule } from './navbar-module/navbar-module.module';
import { HttpClientModule } from "@angular/common/http";    

@NgModule({
declarations: [
  AppComponent,
  HeaderComponent
],
imports: [
  BrowserModule, 
  HttpClientModule,
  NavbarModuleModule
],
providers: [],
  bootstrap: [AppComponent, HeaderComponent, MainComponent]
})

export class AppModule { }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM