简体   繁体   English

Angular 4 找不到在模块内创建的组件

[英]Angular 4 can't find component created inside a module

I have a pretty simple question.我有一个很简单的问题。

I created a new angular project using the cli.我使用 cli 创建了一个新的 angular 项目。 I then created a module and component called home-page.然后我创建了一个名为 home-page 的模块和组件。 Within homepage, I created a component called banner.在主页中,我创建了一个名为横幅的组件。

My main application can import the component just fine.我的主应用程序可以很好地导入组件。 But home page can't seem to import .但是主页似乎无法导入 .

Here's my project structure :这是我的项目结构:

├── app.component.html
├── app.component.sass
├── app.component.spec.ts
├── app.component.ts
├── app.module.ts
├── home-page
│   ├── banner
│   │   ├── banner.component.html
│   │   ├── banner.component.sass
│   │   ├── banner.component.spec.ts
│   │   └── banner.component.ts
│   ├── home-page.component.html
│   ├── home-page.component.sass
│   ├── home-page.component.spec.ts
│   ├── home-page.component.ts
│   └── home-page.module.ts
└── simple-grid.scss

Here's the home-page.component.ts:这是 home-page.component.ts:

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

@Component({
  selector: 'home-page',
  templateUrl: './home-page.component.html',
  styleUrls: ['./home-page.component.sass']
})
export class HomePageComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

Here's the home-page.module.ts:这是主页.module.ts:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { BannerComponent } from './banner/banner.component';
import { HomePageComponent } from './home-page.component';

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [
    BannerComponent,
    HomePageComponent
  ]
})
export class HomePageModule { }

home-page.component.html:主页.component.html:

<div>
  <banner></banner>

</div>

banner.component.ts:横幅.component.ts:

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

@Component({
  selector: 'banner',
  templateUrl: './banner.component.html',
  styleUrls: ['./banner.component.sass']
})
export class BannerComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

banner.component.html:横幅.component.html:

<div id="app" class="container">
  <div class="row">
    <div class="col-4 center"><p>1</p></div>
    <div class="col-4 center"><p>2</p></div>
    <div class="col-4 center"><p>3</p></div>
  </div>
</div>

I know this type of question has been asked before, but they looked like they were asking about stranger imports.我知道以前有人问过这种类型的问题,但他们看起来像是在问陌生人进口。 I think mine should be a lot more basic, so I'd like to know which step I missed or if I have a typo or something.我认为我的应该更基本,所以我想知道我错过了哪一步,或者我是否有错别字之类的。

Thanks谢谢

You are missing an export statement in your HomePageModule您的HomePageModule中缺少export语句

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { BannerComponent } from './banner/banner.component';
import { HomePageComponent } from './home-page.component';

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [
    BannerComponent,
    HomePageComponent
  ],
/////////////// added  below    ////////////////////////////////////
exports: [
    BannerComponent,
    HomePageComponent
  ]
})
export class HomePageModule { }

I found the solution!我找到了解决方案!

So Basically,所以基本上,

I was importing home-page.component into app.module and couldn't get banner.component to load because the home-page.component wasn't exporting it.我正在将 home-page.component 导入 app.module 并且无法加载 banner.component 因为 home-page.component 没有导出它。

Importing home-page.module into app.module allowed me export its child components and worked without issue.将 home-page.module 导入 app.module 允许我导出它的子组件并且正常工作。

reposting the file structure for clarity:为了清楚起见,重新发布文件结构:

├── app.component.html
├── app.component.sass
├── app.component.spec.ts
├── app.component.ts
├── app.module.ts
├── home-page
│   ├── banner
│   │   ├── banner.component.html
│   │   ├── banner.component.sass
│   │   ├── banner.component.spec.ts
│   │   └── banner.component.ts
│   ├── home-page.component.html
│   ├── home-page.component.sass
│   ├── home-page.component.spec.ts
│   ├── home-page.component.ts
│   └── home-page.module.ts
└── simple-grid.scss

TLDR; TLDR; I lack experience with this framework and I hope this helps somebody.我缺乏这个框架的经验,我希望这对某人有所帮助。

Sometimes the issues are so petty that we do not care to look into that.有时这些问题是如此的小,以至于我们不想去研究它。

Check whether folder names are proper or not.检查文件夹名称是否正确。 Path provided in the module.ts for importing that component is proper or not. module.ts 中提供的用于导入该组件的路径是否正确。

eg import { SomeComponent } from './Some-Folder/';例如import { SomeComponent } from './Some-Folder/'; However, SomeComponent is present in 'some-folder' not in 'Some-Folder' then it can give that issue.但是, SomeComponent 存在于 'some-folder' 中而不是 'Some-Folder' 中,那么它就会出现这个问题。 Character case must be same.字符大小写必须相同。

In windows, it may compile successfully but in linux environment it will definitely fail.在windows下,它可能编译成功,但在linux环境下它肯定会失败。

Provided this answers to those who have spent hours for this issue and came here after seeing the issue mentioned in the header.为那些花费数小时解决此问题并在看到标题中提到的问题后来到这里的人提供此答案。

Hope this is helpful to someone.希望这对某人有帮助。

PS Just make sure to create the component using cli command only. PS 只需确保仅使用 cli 命令创建组件。 It will save others valuable time.这将节省其他人的宝贵时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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