代码之家  ›  专栏  ›  技术社区  ›  Wai Yan Hein

子模块的角度5布线不工作

  •  0
  • Wai Yan Hein  · 技术社区  · 6 年前

    我正在使用Angular 5开发一个Web应用程序。我不太会打字。现在,我正在为我的应用程序模块配置路由。但它不起作用,并且抛出了一个错误。

    这是我的 index.html 文件

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>Web</title>
      <base href="/">
    
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="icon" type="image/x-icon" href="favicon.ico">
    </head>
    <body>
      <app-root></app-root>
      <router-outlet></router-outlet>
    </body>
    </html>
    

    我创建了一个 app-routing.module.ts 应用程序文件夹下的文件。

        import { NgModule } from '@angular/core';
        import { Routes, RouterModule } from '@angular/router';
        import { AppComponent } from './app.component';
    
    
        const routes: Routes = [ 
            { path: '', loadChildren: './web/web.module#WebModule' },
        ];
    
        @NgModule({
            imports: [RouterModule.forRoot(routes)],
            exports: [RouterModule]
        })
        export class AppRoutingModule {}
    
    
    Import that routing class in the app.module.ts file
    
    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { RouterModule, Route } from '@angular/router';
    import { AppRoutingModule } from './app-routing.module'
    
    import { AppComponent } from './app.component';
    
    
    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        AppRoutingModule,
        BrowserModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    

    然后创建了另一个模块, web/web.module.ts

    import { NgModule } from '@angular/core';
    import { CommonModule } from '@angular/common';
    import { HomeComponent } from './home/home.component';
    import { WebRoutingModule  } from './web-routing.module';
    
    @NgModule({
      imports: [
        CommonModule,
        WebRoutingModule
      ],
      declarations: [HomeComponent]
    })
    export class WebModule { }
    

    这是我的 web-routing.module.ts

    import { NgModule } from '@angular/core';
    import { Routes, RouterModule } from '@angular/router';
    import { HomeComponent } from './home/home.component';
    
    const routes: Routes = [
        { path: '', component: HomeComponent }
    ];
    
    @NgModule({
        imports: [RouterModule.forChild(routes)],
        exports: [RouterModule]
    })
    export class WebRoutingModule { }
    

    如您所见,我正在渲染 HomeComponent 。我创建了主组件用户 web/home/ 文件夹

    根据我的密码,如果我像这样访问我的网站, localhost:4200 ,它应该将路由器中的主组件HTML呈现在 指数html 文件现在它不会呈现主组件HTML。它正在呈现索引。html但没有主页,路由器出口中的组件被替换。那么,我的代码出了什么问题,如何修复它呢?

    2 回复  |  直到 6 年前
        1
  •  8
  •   Skel    6 年前

    <router-outlet></router-outlet> 必须在标记中 app-home 不在索引中。html文件以使其正常工作

    我刚刚在我的一个项目中设置了路由。

    我的 app.component.ts 看起来像这样

    import { Component } from '@angular/core';
    import { Router } from '@angular/router';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.scss']
    })
    
    export class AppComponent {
    }
    

    我的 app.component.html 看起来像这样

    <router-outlet></router-outlet>
    

    我的 app-routing.module.ts 像这样

    import { NgModule } from '@angular/core';
    import { Routes, RouterModule } from '@angular/router';
    
    // Layouts
    import { MasterComponent } from './components/layouts/master/master.component';
    // Main Pages
    import { HomeComponent } from './components/pages/home/home.component';
    import { PageNotFoundComponent } from './components/pages/page-not-found/page-not-found.component';
    
    const routes: Routes = [
      { path: '', component: MasterComponent, children: [
        { path: '', component: HomeComponent },
      ] },
      { path: '**', component: PageNotFoundComponent },
    ];
    
    @NgModule({
      imports: [RouterModule.forRoot(routes)],
      exports: [RouterModule]
    })
    export class AppRoutingModule { }
    

    还有我的索引。html文件如下所示

    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>Office</title>
      <base href="/">
    
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="icon" type="image/x-icon" href="favicon.ico">
    </head>
    <body>
      <app-root></app-root>
    </body>
    </html>
    
        2
  •  0
  •   kzrfaisal    6 年前

    尝试提供您的web。应用程序中的模块。模块的导入阵列