gpt4 book ai didi

Angular 路由不起作用。抛出错误的组件不是模块的一部分

转载 作者:行者123 更新时间:2023-12-02 16:26:10 25 4
gpt4 key购买 nike

我正在创建 Angular 4 应用程序并尝试实现基本路由。我的应用程序编译正常,但我的路由不起作用

我收到错误 Component HomeComponent 不是任何 NgModule 的一部分,或者该模块尚未导入到您的模块中。

我尚未配置 routerlinks,但假设路由应该根据浏览器中的请求进行工作。

当我单击主页时,home.component.html 的内容应该类似地加载单击“编辑”、“新建”、“电影”应该分别执行相同的操作

有人可以告诉我我的进口是否出了问题。具体来说

包含电影、主页、编辑和新内容

这是我的文件夹的结构

enter image description here

app.module.ts

    import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import {FormsModule} from '@angular/forms';
import {HttpModule} from '@angular/http'
import { AppComponent } from './app.component';
import { NavbarComponent } from './navbar/navbar.component';
import { TopbarComponent } from './topbar/topbar.component';
import { FooterbarComponent } from './footerbar/footerbar.component';
import { MRDBGlobalConstants } from './shared/mrdb.global.constants';
import {AppRoutingModule} from './approuting.module';
import {HomeModule} from './home/home.module';
import {MovieModule} from './movie/movie.module';
import { MRDBCommonService } from './shared/services/mrdb.common.service';
import { NotFoundComponent } from './not-found/not-found.component';
import { SharedModule } from './shared/shared.module';


@NgModule({
declarations: [
AppComponent,
FooterbarComponent,
TopbarComponent,
NavbarComponent,
NotFoundComponent
],
imports: [
BrowserModule,
HttpModule,
SharedModule,
AppRoutingModule
],
providers: [MRDBGlobalConstants,
MRDBCommonService],
bootstrap: [AppComponent]
})
export class AppModule { }

approuting.module.ts

import {NgModule} from '@angular/core';
import {Routes,RouterModule} from '@angular/router';
import {MovieComponent} from './movie/movie.component';
import {HomeComponent} from '../app/home/home.component';
import {NotFoundComponent} from './not-found/not-found.component';
import {NewmovieComponent} from './movie/new/newmovie.component';
import {EditmovieComponent} from './movie/edit/editmovie.component';

export const routes: Routes = [
{path : '', component : HomeComponent},
{path: 'movie', component : MovieComponent},
{path : 'new' , component : NewmovieComponent },
{path : 'edit' , component : EditmovieComponent },
{path: '**',component : NotFoundComponent}

];

@NgModule({
imports: [RouterModule.forRoot(routes,{useHash: true})],
exports: [RouterModule]

})

export class AppRoutingModule{}

home.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HomeComponent } from './home.component';

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

Home.component.ts

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

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

constructor() { }

ngOnInit() {
}

}

最佳答案

在 app.module.ts 的导入中添加 HomeModule

imports: [
BrowserModule,
HttpModule,
SharedModule,
HomeModule,
AppRoutingModule
],

关于 Angular 路由不起作用。抛出错误的组件不是模块的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47164555/

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