【问题标题】:How to setup routes in custom angular library from application如何从应用程序在自定义角度库中设置路由
【发布时间】:2020-09-08 02:13:28
【问题描述】:

我想创建一个库以便能够在多个应用程序中使用它。该库将是一个允许您使用门户进行连接的模块。我想设置配置以从应用程序创建此模块,配置必须能够覆盖路由来源(例如:'authentification' 可以被'auth'覆盖)。所以我写了这个模块:

    { path:  'authentification', component:  StyleRootComponent, children: [
        {
            path:  'portail',
            component:  PortailComponent
        },
        {
            path:  'passwordForgotten',
            component:  PasswordForgotenComponent
        }
        ]
    }
];

@NgModule({
    declarations: [StyleRootComponent, PortailComponent, PasswordForgotenComponent],
    imports: [
        RouterModule.forChild(appRoutes),
        AmplifyAngularModule,
        FormsModule,
        MatFormFieldModule,
        MatInputModule,
        MatCardModule,
        BrowserAnimationsModule,
        MatButtonModule
    ],
    exports: [StyleRootComponent]
})
export class LibraryConnectionModule { 
    static forRoot(config: any): ModuleWithProviders {

        appRoutes[0].path = config.urlAuth;

        return {
            ngModule: LibraryConnectionModule,
            providers: [
            AmplifyService,
            AuthentificationService,
            {
                provide: CognitoConfigsService,
                useValue: config.cognitoConfigs
            }
            ]
        }
    }
}

我可以从应用程序中导入它

imports: [...,
LibraryConnectionModule.forRoot({cognitoConfigs : {}, urlAuth : "auth"}),
...,
]

JIT 的所有工作都像预期的那样,但是当我尝试为生产 (AOT) 构建时,我收到了这个错误:
“AppModule”模板编译期间出现错误
装饰器不支持函数调用,但调用了“LibraryConnectionModule”。

我知道问题出在这条线上(我真的不知道为什么):appRoutes[0].path = config.urlAuth;
所以在生产中我无法从应用程序中选择我的组件的 url...
有人知道我该怎么做吗? (我愿意接受任何建议)

提前致谢 最大

【问题讨论】:

    标签: angular configuration angular-library


    【解决方案1】:

    tsconfig.json 中使用此配置使编译错误消失,我的应用程序和库现在可以工作:我可以在我的主应用程序中loadChlidren 我的库模块(具有路由)。

    "skipTemplateCodegen": true,
    "strictMetadataEmit": true,
    "fullTemplateTypeCheck": true
    

    来自https://github.com/angular/angular/issues/23609#issuecomment-386876693
    这些选项的文档在这里:https://angular.io/guide/angular-compiler-options#fulltemplatetypecheck

    【讨论】:

      猜你喜欢
      • 2012-11-10
      • 1970-01-01
      • 2015-01-22
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多