【发布时间】:2017-11-20 19:47:12
【问题描述】:
如何在页面加载时使用动态 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”正在监听时有效,但是当我重新启动 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)
我能做什么?
【问题讨论】:
-
dynamicmodule or copmonents? -
最好是动态模块
标签: angular components