角度路由 - 编译错误

时间:2017-11-06 14:34:49

标签: angular typescript

我的角度应用程序中有一个错误,我修正了不正确的方法。所以这不是一个紧迫的问题。只是想知道原因,所以我清理我的代码

我有一个包含路由配置的类,以及一些查询路由,填充路由参数,获取路由的函数,......

我的所有功能和属性都是静态的。

static _routes: { [key: string]: Route } = {
    'home' : { path : '', redirectTo : 'client', pathMatch : 'full' },
    'client' : { path : 'client', component : ClientComponent },
    'mandats' : { path : 'mandats', component : MandatsComponent }
};

static route_a: Routes = [
    { path : '', redirectTo : 'client', pathMatch : 'full' },
    { path : 'client', component : ClientComponent },
    { path : 'mandats', component : MandatsComponent }
];

static routes(): Routes {
    return Object.values( AppRoutes._routes );
}

static route( name: string ): Route {
    return AppRoutes._routes[ name ] ? AppRoutes._routes[ name ] : null;
}

static routeUrl( name: string, args: object ): string {
    const route = Object.assign( {}, AppRoutes.route( name ) );
    Object.keys( args ).forEach( key => route.path.replace( `:${key}`, args[ key ] ) );
    return route.path;
}

Screenshot of my code

问题是:当我使用我的routes()函数时,我得到一个compile error 我不明白为什么,我现在使用属性route_a保存我的路由,并且两个方法都返回相同的内容时工作。

我尝试了什么:

  • 我的_routes属性是私有的,我尝试将其设置为公开
  • 将我的功能设置为吸气剂
  • 记录功能的结果 - >预期结果:对象数组

感谢任何可以提供帮助的人

1 个答案:

答案 0 :(得分:0)

您链接的错误是指loadChildren,因此我假设您正在使用提前编译。在这种模式下,编译器的能力在某些方面受到限制。它需要能够静态地分析路由,因此使用routes()函数的结果 - 而不是静态定义的routes_a对象 - 可能会使它混淆。

检查运行Just-in-Time编译是否有效。如果是这样,我怀疑这是你面临的问题。有关AOT编译器限制的更多示例,请查看this reference。不要忘记让我们知道你发现了什么,这样我们就可以从你的xp中学习。

相关问题