找不到模块(Angular 2)

时间:2017-03-19 19:48:11

标签: angular


我想用延迟加载创建一些路由。但我无法摆脱错误

  

无法找到模块app / search / search.module

文件夹“搜索”位于“app”文件夹中,路径正确。它在没有webpack的情况下工作,这个问题在webpack配置之后开始。
它没有延迟加载。
问题在哪里?

app.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent }  from './app.component';
import { RouterModule, Routes } from '@angular/router';

const appRoutes: Routes = [
  { path: 'search', loadChildren: 'app/search/search.module#SearchModule' },
  { path: '**', redirectTo: '/search' }
];

@NgModule({
  imports: [
    BrowserModule,
    RouterModule.forRoot(appRoutes)
  ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

search.module.ts

import { NgModule }      from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { SearchComponent } from "./search.component";
import { LocationsComponent } from "./locations/locations.component";
import { CommonModule } from "@angular/common";
import { GoogleMapsService } from "../shared/services/google-maps.service";

const routes: Routes = [
  { path: '', component: SearchComponent },
  { path: '**', redirectTo: '' }
];

@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild(routes)
  ],
  exports: [
    RouterModule
  ],
  declarations: [ SearchComponent, LocationsComponent ],
  providers: [ GoogleMapsService ]
})
export class SearchModule { }

2 个答案:

答案 0 :(得分:1)

试试这个

const routes: Routes = [
        {path: '', component: SearchComponent, children: [
            {path: '**', redirectTo: ''}
        ]}
    ]

答案 1 :(得分:0)

我认为您应该将search.module.ts添加到app.module.ts并将SearchModule放入declarations

或尝试将./添加到此{ path: 'search', loadChildren: 'app/search/search.module#SearchModule' },。为了得到这个结果:

{ path: 'search', loadChildren: './app/search/search.module#SearchModule' },