【问题标题】:Angular 4 Routing IssuesAngular 4 路由问题
【发布时间】:2017-09-06 18:11:21
【问题描述】:

Angular 似乎没有将我配置的组件加载到空的 '' 路径。

这是我的项目设置:

project/
|- app/
|   |- landing-page/
|   |- second-page/
|   |- third-page/
|   |- app-routing.module.ts
|   |- app.component.html
|   |- app.component.ts
|   `- app.module.ts
|- index.html
`- main.ts

app-routing.component.ts:

const appRoutes: Routes = [{
        path: '',
        loadChildren: 'app/landing-page/landing-page.module#LandingPageModule'
    },{
        path: 'second',
        loadChildren: 'app/second-page/second-page.module#SecondPageModule'
    },{
        path: 'third',
        loadChildren: 'app/third-page/third-page.module#ThirdPageModule'
    },{
        path: '**',
        redirectTo: '/'
}]

@NgModule({
    imports: [
        RouterModule.forRoot(
            appRoutes,
           { enableTracing: true }
        )
    ],
    exports: [
        RouterModule
    ]
})
export class AppRoutingModule { }

app.component.html:

<router-outlet></router-outlet>

index.html:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>AnguTest</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>

landing-page.component.ts(所有 3 个组件都是这样声明的):

const landingRoutes: Routes = [
  { path: '',  component: LandingPageComponent }
]
export const landingRouting = RouterModule.forChild(landingRoutes)

@NgModule({
  imports: [
    CommonModule,
    landingRouting
  ],
  declarations: [LandingPageComponent]
})
export class LandingPageModule { }

问题是每当我在浏览器中导航到应用程序时,输出总是:

second-page works!

即使我将网址更改为http://localhost:4200/http://localhost:4200/third,我也只能看到第二页的输出。

我已经尝试重新排列路由声明顺序,但它仍然是第二页。

如果有帮助,我已将 Chrome 控制台的输出包含在内。看起来该应用正在正确识别路线。

Router Event: NavigationStart
NavigationStart(id: 1, url: '/')
NavigationStart
Angular is running in the development mode. Call enableProdMode() to enable the production mode.
Router Event: RoutesRecognized
RoutesRecognized(id: 1, url: '/', urlAfterRedirects: '/', state: Route(url:'', path:'') { Route(url:'', path:'') } )
RoutesRecognized
Router Event: GuardsCheckStart
GuardsCheckStart(id: 1, url: '/', urlAfterRedirects: '/', state: Route(url:'', path:'') { Route(url:'', path:'') } )
GuardsCheckStart
Router Event: GuardsCheckEnd
GuardsCheckEnd(id: 1, url: '/', urlAfterRedirects: '/', state: Route(url:'', path:'') { Route(url:'', path:'') } , shouldActivate: true)
GuardsCheckEnd
Router Event: ResolveStart
ResolveStart(id: 1, url: '/', urlAfterRedirects: '/', state: Route(url:'', path:'') { Route(url:'', path:'') } )
ResolveStart
Router Event: ResolveEnd
ResolveEnd(id: 1, url: '/', urlAfterRedirects: '/', state: Route(url:'', path:'') { Route(url:'', path:'') } )
ResolveEnd
Router Event: NavigationEnd
NavigationEnd(id: 1, url: '/', urlAfterRedirects: '/')
NavigationEnd

========== 已解决==========

问题是我实际上并没有在我的所有模块中声明路由配置:

// this bit....
const landingRoutes: Routes = [
  { path: '',  component: LandingPageComponent }
]
export const landingRouting = RouterModule.forChild(landingRoutes) 

空路径 '' 不起作用,因为我正在导入我在 app.module.ts 中路由到的模块,有效地进行了双重导入并导致奇怪的行为。

从导入列表中删除 FirstPageModuleSecondPageModule 后,问题得到解决。

有关详细信息,请参阅此答案: Default route redirect not working for lazy loaded routes in Angular 2

【问题讨论】:

  • 不确定是否可以延迟加载引导的第一个模块
  • 我实际上不太确定这个配置会导致延迟加载。我应该尝试更改loadChildren -> component 吗?

标签: angular angular2-routing


【解决方案1】:

尝试将路线更改为类似的东西

  const appRoutes: Routes = [
    {
      path:'',
      redirectTo: 'first',
      pathMatch: 'full' 
    },
    {
      path: 'first',
      loadChildren: 'app/landing-page/landing-page.module#LandingPageModule'
    }, 
    {
     path: 'second',
     loadChildren: 'app/second-page/second-page.module#SecondPageModule'
    }, 
    {
     path: 'third',
     loadChildren: 'app/third-page/third-page.module#ThirdPageModule'
   }, 
   {
     path: '**',
     redirectTo: '/'
  }];

【讨论】:

  • 哇,这很奇怪。我更新以符合您的建议。同样的事情——所有路由只输出second-page works! 除了http://localhost:4200/first,它实际上显示了first-page.component.html
  • @Mac 我没有得到错误你能把错误说清楚一点
  • http://localhost:4200/ 路由到second.component.html... http://localhost:4200/second 路由到second.component.html... http://localhost:4200/third 路由到second.component.html... http://localhost:4200/first 路由到landing.component.html。 ..
  • 为什么将通配符路由路由到'/' 更好地使用 404 tyoe 组件或通配符组件你可以试试吗?
  • 我路由到“/”只是为了尝试加载登录页面。我去掉了通配符路由,并没有影响问题。
猜你喜欢
  • 2017-10-08
  • 2017-11-22
  • 1970-01-01
  • 1970-01-01
  • 2016-06-11
  • 2018-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多