如何在页面加载时使用动态NgModule引导组件?
背景:我在一个应用程序中有一个名为“website.url”的页面或一个名为test.website.url的子域名。当调用子域时,我想基于它自己的NgModule(LpModule)显示登陆页面。默认模块是AppModule。
我试图在main.ts中存档,如下所示:
import {enableProdMode} from '@angular/core';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app/app.module';
import {environment} from './environments/environment';
import {LandingpageModule} from "./app-lp/components/landingpage.module";
if (environment.production) {
enableProdMode();
}
const hostname = window.location.hostname;
const secondLevelDomain = hostname.split('.').reverse()[1];
// let module = AppModule;
// if(secondLevelDomain && secondLevelDomain !== 'www') {
// module = LandingpageModule;
// }
let module = loadModuleByDomain();
platformBrowserDynamic().bootstrapModule(module);
function loadModuleByDomain() {
const hostname = window.location.hostname;
const secondLevelDomain = hostname.split('.').reverse()[1];
if(secondLevelDomain && secondLevelDomain !== 'www') {
return LandingpageModule;
} else {
return AppModule;
}
}
当“ng serve”正在收听时它起作用,但是当我重新启动服务时,会出现以下错误:
试图找到引导代码,但不能。静态指定 可分析的引导代码或将entryModule传递给插件 选项。错误:试图找到引导程序代码,但不能。指定 要么是静态可分析的引导代码,要么是传入entryModule 插件选项。
at Object.resolveEntryModuleFromMain (C:\xampp\htdocs\projekte\ontavio\talentstorm\client\node_modules\@ngtools\webpack\src\entry_resolver.js:128:11) at AotPlugin._setupOptions (C:\xampp\htdocs\projekte\ontavio\talentstorm\client\node_modules\@ngtools\webpack\src\plugin.js:142:50) at new AotPlugin (C:\xampp\htdocs\projekte\ontavio\talentstorm\client\node_modules\@ngtools\webpack\src\plugin.js:26:14) at _createAotPlugin (C:\xampp\htdocs\projekte\ontavio\talentstorm\client\node_modules\@angular\cli\models\webpack-configs\typescript.js:55:12) at Object.exports.getNonAotConfig (C:\xampp\htdocs\projekte\ontavio\talentstorm\client\node_modules\@angular\cli\models\webpack-configs\typescript.js:70:19) at NgCliWebpackConfig.buildConfig (C:\xampp\htdocs\projekte\ontavio\talentstorm\client\node_modules\@angular\cli\models\webpack-config.js:27:37) at Class.run (C:\xampp\htdocs\projekte\ontavio\talentstorm\client\node_modules\@angular\cli\tasks\serve.js:37:98) at check_port_1.checkPort.then.port (C:\xampp\htdocs\projekte\ontavio\talentstorm\client\node_modules\@angular\cli\commands\serve.js:103:26) at <anonymous> at process._tickCallback (internal/process/next_tick.js:169:7)
我该怎么办?
答案 0 :(得分:3)
经过长时间的长时间搜索,我今天找到了一个解决方案:
https://github.com/angular/angular-cli/issues/2887#issuecomment-281168941
我已经创建了一个额外的ts文件(main-lp.ts)并添加了以下代码:
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {LandingpageModule} from "./app-lp/components/landingpage.module";
export function bootstrapLandingpageModule() {
platformBrowserDynamic().bootstrapModule(LandingpageModule);
}
在main.ts中,我使用了以下代码并且它有效:
import {enableProdMode} from '@angular/core';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {environment} from './environments/environment';
import {AppModule} from './app/app.module';
import {bootstrapLandingpageModule} from "./main-lp";
if (environment.production) {
enableProdMode();
}
if(!isSubDomain()) {
platformBrowserDynamic().bootstrapModule(AppModule);
} else {
bootstrapLandingpageModule();
}
function isSubDomain() {
const hostname = window.location.hostname;
const secondLevelDomain = hostname.split('.').reverse()[1];
return !!(secondLevelDomain && secondLevelDomain !== 'www');
}