如何在頁面加載中使用動態NgModule引導組件?Angular 4 - 帶動態模塊的引導程序根組件
背景:我在一個應用程序中有一個由「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服務」是聽,但是當我重新啓動納克服務有以下錯誤:
Tried to find bootstrap code, but could not. Specify either statically analyzable bootstrap code or pass in an entryModule to the plugins options. Error: Tried to find bootstrap code, but could not. Specify either statically analyzable bootstrap code or pass in an entryModule to the plugins options.
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)
我該怎麼辦?
'dynamicmodule或c opmonents'? – Aravind
最好是dynamicmodule – Turbo