【问题标题】:Angular4 - Router module provider AOTAngular4 - 路由器模块提供商 AOT
【发布时间】:2017-10-06 14:25:15
【问题描述】:

这是我对路由器提供者的声明:

@NgModule({
    imports: [RouterModule.forRoot(RoutesFactory.buildRoutes())],
    exports: [RouterModule]
})
export class RouterModuleProvider { };

在 AOT 中编译,我得到这个错误:

静态解析符号值时遇到错误。调用函数 'getRoutes',不支持函数调用。考虑更换 引用导出函数的函数或 lambda,解析 符号RouterModuleProvider

这里是RoutesFactory.buildRoutes的内容

static buildRoutes() {
    let returnRoutes: Routes = [
        { path: '', redirectTo: '/login', pathMatch: 'full' },
        { path: 'login', component: ViewsModule.LoginViewComponent }
    ];

    let appendRoutes = (items: MenuItem[], routes: Routes) => {
        for (let item of items) {
            let route: Route = {
                path: item.Path, component: item.Component, children: []
            };

            if (item.Children != null)
                appendRoutes(item.Children, route.children)

            routes.push(route);
        }
    };

    appendRoutes(RoutesFactory.getMenus(), returnRoutes);

    return returnRoutes;

}

RoutesFactory.getMenus() 返回一个静态内部对象数组。 (没有从后端加载)

有没有办法解决这个问题?

【问题讨论】:

  • RoutesFactory.buildRoutes() 中有什么?
  • 我编辑了自己的问题,以便提供有关该问题的更多详细信息
  • 这看起来是一种非常奇怪的aot路由方式。你这样做有什么理由吗?
  • 它指向错误中的函数'getRoutes',你在哪里调用它>
  • @mast3rd3mon 原因是我根据菜单对象创建路由。这是唯一的原因。我愿意接受任何建议

标签: angular aot


【解决方案1】:

不幸的是,正如错误所暗示的那样,元数据中的所有内容(在@NgModule(...) 声明中)都必须是静态可解析的。这意味着在 buildRoutes 函数中不能有任何逻辑,只有静态声明。

但是,似乎有一种专门用于动态定义路由的替代方法。请看这个答案:Dynamic routing based on external data

【讨论】:

  • 我会看看你的建议。感谢您的提示。
猜你喜欢
  • 2018-03-14
  • 2017-12-07
  • 2017-08-07
  • 2017-07-26
  • 2018-02-28
  • 2017-09-26
  • 2023-03-24
  • 2019-04-17
  • 2015-12-09
相关资源
最近更新 更多